Pages

Tuesday, 31 July 2012

Variable

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

Syntax:
                datatype identifier = value(if initialized)
same as,
             int a = 2;

The identifier is the name of the variable. To declare more than one variable of the specified type, use a comma-separated list.

Here are several examples of variable declarations of various types. Note that some include an initialization.

int a, b,c;                         // declare three int a, b and c.
int d=2, e=4, f=6;            // declare three more variables.
                                     // and initializing d and f.
byte z=22;                     // declare and initializes z.
double pi = 3.14159;       // double and float are used to store decimal values.
char x = 'x';                   // declare char and the value stored in
                                   // it will b a character.
String name = "Admin"   // String is not a datatype, it is a class but it is
                                  // used as a datatype.

No comments:

Post a Comment