Tokens in Java


What is Tokens?Which types of Tokens used in Java.

In a Java program, all characters are grouped into symbols called tokens.

Tokens are the various Java program elements which are identified by the compiler. A token is the smallest element of a program that is meaningful to the compiler.

When you compile a program, the compiler scans the text in your source code and extracts individual tokens. While tokenizing the source file, the compiler recognizes and subsequently removes white spaces (spaces, tabs, newline and form feeds) and the text enclosed within comments. E.g

Public class Hello

{

Public static void main(String args[])

{

System.out.println(“welcome in Java”);   //print welcome in java

}

}

In above Example ,the source code contains tokens such as public, class, Hello, {, public, static, void, main, (, String, [], args, {, System, out, println, (, “welcome in Java”, }, }. The resulting tokens· are compiled into Java bytecodes that is capable of being run from within an interpreted java environment. Token are useful for compiler to detect errors. When tokens are not arranged in a particular sequence, the compiler generates an error message. Java language includes five types of tokens and they are:

·         Reserved Keywords

·         Identifiers

·         Literals

·         Variables

·         Operators

tokens

 

  • Reserved Keywords – Reserved keywords are java tokens with predefined meaning. Java has 60 reserved keywords.e.g int,class,break,continue etc.
  • Identifiers – Identifiers are java tokens designed and decided by the java programmer. Examples for java tokens namely identifiers are: name for the class, name for members of the class, and temporary variables in class methods.
  • Literals – Literals are java tokens containing set of characters. Literals are used to represent a constant that has to be stored in a variable.e.g 2,5,4.6,”ram”,’s’ etc.
  • Operators – Operators are java tokens containing a special symbol and predefined meaning in Java. Operators can be used with one or more operands to achieve a result.e.g unary,binary,ternary operators
  • Variables – Variables are used to hold the value.e.g

int a,b;

int is datatype and a,b are integer type variable.

Comments are closed.