3.2 Variables
3.2 Variables
By Upekha Vandebona
What is a Variable?
The variable is the basic unit of storage
in a Java program.
A variable is defined by the
combination of an identifier, a type,
and an optional initializer.
In addition, all variables have a scope,
which defines their visibility, and a
lifetime.
Variable Declaration
In Java, all variables must be
declared before they can be
used.
The basic form of a variable
declaration is:
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
(target-
type) value
Here, target-type specifies the desired type to
convert the specified value to.
For example, the following fragment casts an int
to a byte. If the integer’s value is larger than the
range of a byte, it will be reduced modulo (the
remainder of an integer division by the) byte’s
range.
int a = 7;
byte b;
// …
b = (byte) a;
Cast Operator
To force the compiler to jam the
value of a bigger primitive variable
into a smaller one, you can use the
cast operator. It looks like this:
long y = 42; // so far so good
int x = (int) y; // x = 42 cool!
class MyFirstApp{
public static void main(String args[]) {
int a= 257;
byte b = (byte)a;
System.out.println(b);
}
}
Output?
Truncation
A different type of conversion will occur when a
floating-point value is assigned to an integer
type: truncation.
As you know, integers do not have fractional
components. Thus, when a floating-point value
is assigned to an integer type, the fractional
component is lost.
For example, if the value 1.23 is assigned to an
integer, the resulting value will simply be 1.
The 0.23 will have been truncated.
Truncation
If the size of the whole number
component is too large to fit into the
target integer type, then that value
will be reduced modulo the target
type’s range.
Automatic Type Promotion in
Expressions
Inaddition to assignments, there is
another place where certain type
conversions may occur: in
expressions.
byte a = 40;
byte b = 50; The result of the intermediate term a*
byte c = 100; b easily exceeds the range of
either of its byte operands.
int d = (a * b) / c; To handle this kind of problem, Java
automatically promotes each byte,
short,
or char operand to int when
evaluating an expression.
This means that the subexpression
a*b is performed using integers—not
bytes.
Thus, 2,000, the result of the
intermediate
expression, 50*40, is legal even though
a and b are both specified as type byte.
Automatic Type Promotion in
Expressions
As useful as the automatic
promotions are, they can cause
confusing compile-time errors.
For example, this seemingly correct
code causes a problem:
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a
byte!
Automatic Type Promotion in
Expressions
In cases where you understand the
consequences of overflow, you
should use an explicit cast, such as
byte b = 50;
b = (byte)(b * 2);
which yields the correct value of
100.
The Type Promotion Rules
Java defines several type promotion rules that
apply to expressions.
They are as follows:
First, all byte, short, and char values are promoted
to int, as just described.
Then, if one operand is a long, the whole
expression is promoted to long.
If one operand is a float, the entire expression is
promoted to float.
If any of the operands are double, the result is
double.
class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d *
s));
System.out.println("result = " + result);
}
}
Math.PI has a double
value
Thank You!