Java Reference
In-Depth Information
int credit1=100, credit2=150;
int credit=credit1+credit2;
int debit1=50, debit2=25, debit3=100;
int debit=debit1+debit2+debit3;
int balance=credit-debit;
System.out.print("Balance:");
System.out.println(balance);
This example illustrates two constructions:
-
Variable declaration and assignment to a constant (a
basic expression
—see
for example, variable
credit1
)
-
Variable declaration and assignment to an
arithmetic expression
(see for
example, variable
debit1
).
As we have formerly seen, variables are useful for storing initial values. Thus
these variables play an important role in the initialization of programs. For
example, the former program for computing the volume of a 3D box can be
rewritten as:
Program 1.9
Volume of a 3D box using
double
type variables
class
VerboseVolumeBox2
{
public static void
main ( String [ ] args )
double
width=0.5, height =1.0, depth=0.2;
System . out . print (
"Volume of the box (in cubic meter):"
);
System . out . println ( width
∗
height
∗
depth) ;
}
}
Java distinguishes upper from lower case in variable names, as exemplified
below:
Program 1.10
Java distinguishes upper/lower cases
class
UpperLowerCase
{
public static void
main ( String arguments [ ] )
int
MyVar ;
// myvar variable is different from MyVar
int
myvar ;
// Generate a syntax error at compile time:
// cannot find symbol variable myVar
System . out . println (myVar) ;
}
}
That is, Java is
case-sensitive
.
Search WWH ::
Custom Search