previous | start | next

Read section "Language Basics" from the Java Tutorial,
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

Variables

A variable is an item of data named by an identifier.
The variable's type determines what values it can hold
and what operations can be performed on it.

Variable declaration:

type name
Variable scope: section of code where the variable's simple name can be used.
The scope is determined implicitly by the location of the variable declaration.

Scope Categories:

Example:

  class Myclass {
    private int p;

    // ....
    public void method() {
       int i;
       for (i=0; i<10; i++) {
          int j = i * p;
          // ...
       }

       System.out.println("i=" + i);
       System.out.println("j=" + j);
     }
    }
   }

Variable initialization:

With assignment statement:
    float pi = 3.1415;

Final Variables:

The value of a final variable cannot change after it has been initialized.
Are similar to constants.
    final float e = 2.71828183;

Primitive data types:

Keyword Description Size/Format
(integers)
byte Byte-length integer 8-bit two's complement
short Short integer 16-bit two's complement
int Integer 32-bit two's complement
long Long integer 64-bit two's complement
(real numbers)
float Single-precision floating point 32-bit IEEE 754
double Double-precision floating point 64-bit IEEE 754
(other types)
char A single character 16-bit Unicode character
boolean A boolean value (true or false) true or false

Reference type:

The value of a reference type variable, in contrast to that of a primitive type,
is a reference to (an address of) the value or set of values represented by the
variable:


Arrays, classes, and interfaces are reference types.

    Greeter g1 = new Greeter("Students");

    Greeter g2 = g1;

    int v[] = new int[100]; 


previous | start | next