The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Java Tokens

Tokens are the smallest building blocks of any programming language. Variables, Data Types, Constants, Operators, Method names are examples of tokens. A combination of these tokens and whitespaces form a Java statement and a combination of statements form a Java program. Every statement ends with a semicolon
( ; ) and follows a particular syntax.

Variables and Data Types
Consider the following Java statement :

int sum = 10 ; // initializing a variable sum of data type integer with value 10

Above statement can be divided into tokens ( int, sum, =, 10 )
int is a keyword which refers to integer Data type.
Keywords are predefined identifiers ( names of variables, methods, classes etc. ) that are reserved and have special meaning for a language. Keywords cannot be used as variable, class or methods names.
Variables are reserved memory locations to store values.
Data Types specify what kind of values a variable can store ( In the above statement, sum can store only integer type values since its data type is int ).
Based on the Data Type of a variable, Operating System allocates required memory for that variable.

Data Types in Java can be categorized into 2 classes :-
Primitive Data Types [ byte, short, int, long, float, double, char, boolean ]
Non-Primitive Data Types [ String, Array, Vector, etc. ]

Following table lists down the C++ built-in data types and other attributes.

Data TypeDefault ValueBit widthRange
byte01 Byte-128 to 127
short02 Bytes-32,768 to 32,767
int04 Bytes-2,147,483,648 to 2,147,483,647
long0L8 Bytes-2^63 to 2^63-1
float0.0f4 Bytes3.4e-038 to 3.4e+038
double0.0d8 Bytes1.7e-308 to 1.7e+308
booleanfalse1 BitTrue (1) or False (0)
char\u00002 Bytes0 to 65535

Variables
Every memory location used to store some values is assigned an identifier by the programmer. This identifier is known as variable. In other words, variable is a name assigned to a storage location. There are three types of variables in Java.
1 ) Local Variables are declared and used inside methods.
2 ) Instance Variables are declared inside a class but outside any method. Each object has its own copy of      instance variables. So, these variables are created when objects are instantiated.
3 ) Static / Class Variables are global to the class and doesn't belong to any particular object. In other words,      there is one copy of each static variable is created per class when the class is loaded.

Following program illustrates the use of three types of variables :

/*
 * This program demonstrates the use of three types of variables
 */
class Student {
   float total_marks; // instance variable ( one copy per object created )
   static int studObjCount; // class variable ( stores no. of Student objects)
   public void setMarks(float marks) {
      total_marks = marks;
   }
   public void displayAvg() {
      float avg; // local variable ( visible only inside this method )
      avg = total_marks/5;
      System.out.println("Average Marks = " + avg);
   }
}

public class VariableDemo {
   // main method
   public static void main(String []args) {
      Student stud1, stud2;
      stud1 = new Student();         // create 1st student object
      stud1.setMarks(435);           // set the marks of 1st student;
      Student.studObjCount++;        // increment student object count;
      stud2 = new Student();         // create 2nd student object
      stud2.setMarks(353);           // set the marks of 2nd student;
      Student.studObjCount++;        // increment student object count;
      int num_students = Student.studObjCount;
      System.out.println("No. of students = " + num_students);
      /* Display student data */
      stud1.displayAvg();            // display average marks of student 1
      stud2.displayAvg();            // display average marks of student 2
   }
}

In the above program, we have declared a class student with two methods setMarks( ) which takes a float value as argument and displayAvg( ) which divides the marks by 5. There is an instance variable total_marks which stores the marks of a particular student ( each student corresponds to an object ) and a class variable studObjCount which maintains the count of number of objects created. Please note that the class variable is accessed directly using the class name and the methods of Student class are accessed using the objects. We will discuss about classes and objects in detail later in the tutorial.
Another important point to note is that local variables doesn't have default values. So if we do not initialize them explicitly, it will result into compilation error.

Symbolic Constants
Symbolic Constants refer to fixed values that cannot be altered in the program.
If a variable is declared of type final, the value of that variable cannot be modified once defined. The keyword final is a data type modifier. Consider the following declaration :-

final int num = 100 ;

num is a constant and cannot be assigned any other value in the program.
A statement like num = 200; is illegal.

Back | Next