Java Reference
In-Depth Information
is an error. The compiler will complain about an Ȓuninitialized variableȓ when you use
a variable that has never been assigned a value. (See
Figure 2
.)
The remedy is to assign a value to the variable before you use it:
All variables must be initialized before you access them.
int luckyNumber;
luckyNumber = 13;
System.out.println(luckyNumber);
// OK
Or, even better, initialize the variable when you define it.
int luckyNumber = 13;
System.out.println(luckyNumber);
// OK
S
YNTAX
2.2 Assignment
variableNameÉ=Évalue;
Example:
luckyNumber = 12;
Purpose:
To assign a new value to a previously defined variable
S
ELF
C
HECK
4.
Is
12 = 12
a valid expression in the Java language?
5.
How do you change the value of the
greeting
variable to
ÐHello,
Nina!Ñ;
?
2.3 Objects, Classes, and Methods
An object is an entity that you can manipulate in your program. You don't usually
know how the object is organized internally. However, the object has well-defined
behavior, and that is what matters to us when we use it.