The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

## Variable Initialization

> Local variables and member variables can be initialized with an assignment statement when they're declared. The data type of both of the assignment operator's operands must match. The `MaxVariablesDemo` program provides initial values for all of its local variables when they are declared. The variable declarations from that program is shown below with the initialization code in red:

> >     // integers
> >     byte largestByte = Byte.MAX_VALUE;
> >     short largestShort = Short.MAX_VALUE;
> >     int largestInteger = Integer.MAX_VALUE;
> >     long largestLong = Long.MAX_VALUE;

> >     // real numbers
> >     float largestFloat = Float.MAX_VALUE;
> >     double largestDouble = Double.MAX_VALUE;

> >     // other primitive types
> >     char aChar = 'S';
> >     boolean aBoolean = true;

> Method parameters and exception-handler parameters cannot be initialized in this way. The value for a parameter is set by the caller.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form