Variables


Variables are containers for storing data values.

Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:

 String greeting = "Hello";

where type is one of Java’s types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable. To create several variables that store integers, look at the following example:

 int x = 5, y = 6, z = 50;

Identifiers
All Java variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, and totalVolume). The general rules for constructing names for variables (unique identifiers) are
Review: Identifier.java (checking identifier names)
 public class Identifier {
   public static void main( String[ ] args ) {
     int     this = 2;           //  correct    incorrect
     Integer that = 2;           //  correct    incorrect
     short   _under;             //  correct    incorrect
     byte    1UND;               //  correct    incorrect
     String  str@ = "Hello";     //  correct    incorrect
     boolean Bool;               //  correct    incorrect
     Float   number;             //  correct    incorrect
   }
 }
        Result:




      Rise and shine. It is your first day at school.