Tokens in Java | Explained with Examples

Types of Tokens in Java

Tokens are the smallest individual unit of the program , In other words, they are the building blocks of a language.

Basically In Java, the program is a collection of classes and methods, while methods are a collection of various expressions and statements .

Moreover, statements and expressions are made up of tokens. Tokens in Java are the small units of code which a Java compiler uses for constructing statements and expressions.

There are five tokens in Java which are shown below:

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  5. Special Symbols/Separators
Tokens in Java

Let’s take a example of token

    public class ABC  
    {  
    public static void main(String args[])  
    {  
    System.out.println("Technoname");  
    }  
    }  

In the above code , public, class, ABC, {, static, void, main, (, String, args, [, ], ), System, ., out, println, Technoname, etc. are the Java tokens.

The Java compiler (JDK: Java Development Kit) converts/translates this code to Java Bytecode and later this Bytecode translate to machine code with the help of JVM(Java Virtual Machine).

This translation process of a Java program is further explained here.

1 .Keywords :
  • Keywords are the reserved words , that is to say these are the words which are defined by language.
  • For Example : int , float,double etc
  • All keywords in Java are in lowercase
  • Certainly, variable name must not match with any keyword
  • However , there are 32 keywords in C, 63 in C++ and 50 in Java

01. abstract02. boolean03. byte04. break05. class
06. case07. catch08. char09. continue10. default
11. do12. double13. else14. extends15. final
16. finally17. float18. for19. if20. implements
21. import22. instanceof23. int24. interface25. long
26. native27. new28. package29. private30. protected
31. public32. return33. short34. static35. super
36. switch37. synchronized38. this39. throw40. throws
41. transient42. try43. void44. volatile45. while
46. assert47. const48. enum49. goto50. strictfp
Keywords in Java
2 . Identifiers

Identifiers are the name of a variable/ array / method/class/ interface / package.

For Example :

int n;
int arr[];
class ABC
{
}

In above example n,arr,ABC are identifiers.

Identifier naming rules :
  • It can contain alphabets (a-z,A-Z), digits(0-9) or symbols(_,$)
  • It cannot contains space,dot and other special symbols(@,#,& )
  • First letter cannot be a digit
  • It must not match with any keyword of Java
  • Two identifier cannot have same name within same scope
  • There is no limit on length of identifier because big names are more understandable.
  • In conclusion, All the above points can be explained by below examples :
//Valid Identifiers
$technoname  //correct
_techno      //correct
techno       //correct
techno_name  //correct
tech21no     //correct
 
//Invalid Identifiers
techno name    //error
&techno        //error
33techno       //error
float          //error
techno/name    //error
techno's       //error

3. Literals

Literals in Java are similar to normal variables which were explained above but their values cannot be changed once assigned. In other words, literals are constant variables having fixed values.

These are defined by programmer and can belong to any data type. There are five types of literals available in Java :

  1. Integer
  2. Floating Point
  3. Character
  4. String
  5. Boolean
LiteralType
33int
7.89double
false, trueboolean
‘A’, ‘9’, ‘-‘char
“technoname”String
nullany reference type
Literals in Java

Example :

public class TechnoName { 
    public static void main(String[] args) 
    { 
        int tech1 = 33;     // Int literal 
        float tech2 = 33.10;     // Float literal 
        char tech3 = 'tech' // char literal 
        String tech4 = "technoname"; // String literal 
        boolean tech5 = true; // Boolean literal 
 
        System.out.println(tech1); //33
        System.out.println(tech2); //33.10
        System.out.println(tech3); //tech
        System.out.println(tech4); //technoname
        System.out.println(tech5); //true
    } 
}
4. Operators

Basically, operators are the special symbols that tells the compiler to perform a special mathematical and non mathematical operations.

Java provides different types of operators that can be classified based on the functionality they provide.

Moreover,Java supports eight types of operators which are as follows:

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Unary Operators
  • Logical Operators
  • Ternary Operators
  • Bitwise Operators
  • Shift Operators
OperatorSymbols
Arithmetic+ , – , / , * , %
Unary++ , – – , !
Assignment= , += , -= , *= , /= , %= , ^=
Relational==, != , < , >, <= , >=
Logical&& , ||
Ternary(Condition) ? (Statement1) : (Statement2);
Bitwise& , | , ^ , ~
Shift<< , >> , >>>
Operators in Java
5. Separators

The separators in Java is also known as punctuators. They have special meaning known to Java compiler and cannot be used for any other purpose .

Some of the special symbols are shown in below table :

SymbolDescription
Square Brackets []These are used define array elements , single square brackets define one dimensional array while two blocks of square brackets defined two dimensional array.
Parentheses()These indicate a function call along with function parameters
Braces{}The opening and ending curly braces indicate the beginning and end of a block of code .
Comma ( , )This helps in separating more than one statement, value or parameter in an expression
Semi-Colon (;)It is used to separate two statements and used at end of a statement.
Separators in Java

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues.

If you have any questions or feedback, then please drop a note.

To explore more about Java you can refer to our other Java Blogs.

Please check most asked programming questions of strings with examples by clicking here, after that you might get a deep knowledge about string operations

For understanding data structures in a easy way you can check this article in our website.

Leave a Reply

Your email address will not be published. Required fields are marked *