Course Contents:
- Integers
- Floating-point
- Characters
- Boolean
- Variables
- Declaration
- Dynamic initialization
- The scope and lifeline of variables
- Type casting
Variable
A
variable is an identifier that denotes a storage location used to store a data
value. A variable takes different value at a different time during program
execution.
Rules
for constructing variable in Java
- They
must not begin with a digit.
- Uppercase
and lowercase are distinct. This means that Sum is not the same as sum
and SUM.
- It should not be a keyword.
- White
space is not allowed.
- Variable
length can be of any length.
A
variable must be declared before it is used in the program. Declaration does
three things:
1. It
tells the compiler what the variable name is.
2. It
specifies what types of data the variable will hold.
3. The
place of declaration decides the scope of the variable.
Data
types
Java
is strongly typed language. Ever y variable in java has a data type. Data types
represent the size and type of values that can be stored. Java language is rich
in data types. The variety of data types available allows the programmer to select
the type appropriate to the needs of the
application.
Since
java is strongly typed language. In java every variable has a type, every
expression has a type, and every type is strictly defined. All assignments are checked
f or type compatibility.
There
are no automatic conversions of conflicting types as in some language. The java
compiler checks all expressions and parameters to ensure that the types are compatible.
Any type
mismatches are errors that must be corrected before the compiler will finish
compiling the class.
Simple
data types
Java
defines eight simple or built-in data types. These can be put in four groups.
They are:
- Integers
This
group includes byte, short, int and long , which are whole valued
signed numbers.
All
of these are signed positive and negative values. The range and width of the
integer type is given below:
Name
|
Width
|
Range
|
long
|
64
(8 byte)
|
-9,223,372,036,854,775,808
to +9,223,372,036,854,775,807
|
int
|
32
(4 byte)
|
-2,147,483,648
to +2,147,483,648
|
short
|
16
(2 byte)
|
-32768
to 32,767
|
byte
|
8
(1 byte)
|
-128
to +127
|
byte
The
smallest integer type is byte. This is a signed 8 bit type. Variable of type
byte are useful when working with a stream of data from a network or file. Byte
variable are declared by the keyword byte.
e.g.
byte b,c;
Here,
b and c are variable of type byte.
short
short
is a signed 16-bit type. This is the least used data type in java. This is
declared with keyword short e.g. short a; short b; here a and b are
variable of type short.
int
int
is a signed 32-bit type and is most commonly used data types in java. It is declared
with keyword int.
e.g.
int a,b;
long
It is
a signed 64-bit type and is useful for those occasions where and integer type
is not large enough to hold the desired value. The range of long is quite
large. It is useful when big whole numbers are needed.
e.g.
long second, day, month;
- Floating
point numbers
Floating
numbers are also known as real numbers. This group includes float and double
, which represent numbers with fractional precision. These are used when evaluating
expressions that require fractional precision. The width and range are given
below:
Name
|
Width
in bit Range
|
double
|
64
4.9e-324 to 4.9e+324
|
float
|
32
1.4e-o45 to 1.4e+038
|
float
It is
a single precision 32-bit type. It is useful when the values are either very
large or very small. Variable of this type are useful when we need a fractional
component, but not a large no of precision. Floating point variables ar e
declared with keyword float.
e.g.
flaot x,y;
double
It is
a double precision 64 bit data type. Variables of type double are declared with
keyword double.
e.g.
double pi, x,y;
- Characters
This group
includes char , which represents symbols in a character set, like letters
and numbers. This data type is used to represent characters.
- Boolean
Java has
a simple type, called Boolean for logical values. It can have only one of two possible
values, true and false. This is the type returned by all relational operators.
WAP in java that computes the area of circle
WAP in java that computes the area of circle
class
Area
{
public
static void main(String args[])
{
double
pi, r,a;
r=15.5;
pi=3.1416;
a=pi*r*r;
System.out.println(“Area
of circle is “ + a);
}
}
WAP
that demonstrate the use of character variable
class
CharDemo
{
Public
static void main(String args[])
{
Char
ch1,ch2;
Ch1=88;
Ch2=’Y’;
System.out.println(“ch1
and ch2:”);
System.out.println(ch1
+ “ “ +ch2);
}
}
Output:
Ch1
and ch2: X Y
WAP
that demonstrate the character variable behave like integers
Class
CharDemo1
{
Public
static void main(“String args[])
{
Char
ch1=’X’;
System.out.println(“ch1
contains “ +ch1);
Ch1++;
// increment ch1
System.out.println(“ch1
is now “ +ch1);
}
}
Output
Ch1
contains X
Ch1
is now Y
Dynamic
initialization
Java allows
variables to be initialized dynamically, using any expression valid at the time
the variable is declared.
WAP
that demonstrate the concept of dynamic initialization of variables class
MainClass
{
public
static void main(String args[])
{
double
a = 3.0 , b
= 4.0 ;
//
c is dynamically initialized
double
c =
Math.sqrt(a * a + b * b);
System.out.println(
"Hypotenuse is " + c);
}
}
This
program consists of three local variables a, b and c. The first two a and b are
initialized by constants. However, c is initialized dynamically to the result
of the expression calculated by the sqrt function defined inside Math
class.
Scope
and lifetime of variables
Java
allows variables to be declared within any block. A block is begin with opening
curly brace and ended by a closing curly brace. This block defines the scope.
When a new block is created, a new scope is defined. The scope defines the
lifetime of a variable. When a variable is declared inside a scope, then they
are visible to code that is defined within that scope and lifetime of such variable
is within such block.
WAP
in java that demonstrate the scope of the variables
class
Scope
{
public
static void main(String args[])
{
int
x;
x=10;
if(x==10)
{
int
y=20;
System.out.println("x
and y " + x + " " +y);
x=y*2;
}
//y=100;
//error y is not defined here.
// x
is still known here.
System.out.println("x
is " +x);
}
}
Output:
x abd
y 10 20
x is
40
Here,
in this program x is declared at the start of the main and its scope and
lifetime is within the main. Another variable y is declared within if block and
its lifetime and scope is within if block.
Type
Casting
In
java, a value of one type can be stored into a variable of another type. If the
two types are compatible, then java will perform the conversion automatically.
For example, we can assign an int value to long variable. However, all types
are not compatible and all type conversions are not
allowed
implicitly. But it is possible to obtain conversion between incompatible types.
To do such operation, we must use casting, which performs an explicit conversion
between incompatible types. The process of converting one date type to another
is called casting.
Automatic
Type conversion
When
one type of data is assigned to another type of variable, an automatic type
conversion will take place if the following conditions are met:
- The
two types are compatible
- The
destination type is larger than the source type.
e.g. int
type is always large enough to hold all valid byte values, so no
explicit cast statement is required.
byte b=15;
int a
= b;
is
valid statements.
The
process of assigning a smaller type to a larger one is known as widening or
promotion and that of assigning a larger type to a smaller one is known as narrowing
and narrowing might results is loss of information.
From
|
To
|
Byte
|
short,
char, int , long, float, double
|
Short
|
int,
long, float, double
|
Char
|
int,
long float, double
|
Int
|
long,
float, double
|
Long
|
float,
double
|
Float
|
Double
|
Table:
casts that results in no loss of information
Casting
incompatible types
In
java, when a narrowing conversion is required, the programmer must use cast. A
cast is simply an explicit type conversion. The general syntax of casting is target_type
variable1 = (target_type) variable2;
Here,
in this statement, the variable2 is first converted to target_type and then the
resulting value is assigned to the variable1.
e.g.
int
m=15;
byte
n = (byte) m; // casting int to byte
long
count = m;
WAP
that shows the concept of casting
class
Auto
{
public
static void main(String args[])
{
int
b=15;
byte
a = (byte)b; // casting int to byte
long
c = b; // no required to cast because small data is assigned to large variable.
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Output:
15
15
15
No comments:
Post a Comment