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 Type Default Value Bit width Range
byte 0 1 Byte -128 to 127
short 0 2 Bytes -32,768 to 32,767
int 0 4 Bytes -2,147,483,648 to 2,147,483,647
long 0L 8 Bytes -2^63 to 2^63-1
float 0.0f 4 Bytes 3.4e-038 to 3.4e+038
double 0.0d 8 Bytes 1.7e-308 to 1.7e+308
boolean false 1 Bit True (1) or False (0)
char \u0000 2 Bytes 0 to 65535

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.

About    Contact    Sitemap    Terms    Privacy