What is Token in java with Example?

In this post, we will learn What is Token in java? which is very important part of java programming.

What is Token in Java?

Tokens are smallest elements of a program that is identified by a complier.

Every Java methods contain statements and expression which is created using tokens.

Let’s take an example and understand Tokens in java.

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

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

Java compiler translates these tokens into byte code. These byte codes are executed inside the interpreted java environment.

Java supports 5 types of tokens in java which are –

  1. Keywords
  2. Identifiers
  3. Operators
  4. Literals
  5. Special Symbols

We will discuss each token one by one.

Keywords

These are the pre-defined reserved words in a programming language.

Each keyword has a meaning or each keyword perform a specific operation. We cannot use keywords as variable name. These are written in lower case.

Java supports following keywords.

abstract     assert      boolean

break        byte        case

catch        char        class

const        continue    default

do           double      else

enum         exports     extends

final        finally     float

for          goto        if

implements   import      instanceof

int          interface   long

module       native      new

open         opens       package

private      protected   provides

public       requires    return

short        static      strictfp

super        switch      synchronized

this         throw       throws

to           transient   transitive

try          uses        void

volatile     while       with


Identifiers

Java Identifiers are the user defined names of classes, variables, methods, array, packages and interfaces. It uses letters, underscores, digits and $ sign. We cannot use keywords as identifiers because those are reserved for special use.

There are some standards which are must follow while naming the identifiers such as:

  1. The first letter of an identifier must be a letter, underscore and dollor sign ($). It cannot be start with a digit.Example: Abc1$23__23abc$$abc123
  2. Identifiers cannot contain whitespace.
  3. Identifiers in java are case-sensitive.
  4. Keywords cannot use as an identifiers.

Operators

Operators are special symbol which are used to perform some operations. Java has different types of operators that can be classified according to the functionality they provide.

There are 8 types of operators in Java. These are as follows-

  1. Arithmetic Operators
  2. Assignment Operators
  3. Unary Operators
  4. Relational Operators
  5. Logical Operators
  6. Bitwise Operators
  7. Ternary Operators
  8. Shift Operators
OperatorSymbols
Arithmetic+ , – , / , * , %
Unary++ , – – , !
Assignment= , += , -= , *= , /= , %= , ^=
Relational==, != , < , >, <= , >=
Logical&& , ||
Ternary(Condition) ? (Statement1) : (Statement2);
Bitwise& , | , ^ , ~
Shift<< , >> , >>>

Literals

It is also like a normal variable. The difference is only that, value cannot be change once it is defined. We can say it is constant in nature. It can be categorized into integer literal, string literal, Boolean literal.

There are five types of literals are as follows-

  • Integer
  • Floating Point
  • Character
  • String
  • Boolean

Special Symbol: It is a set of few characters which have special meaning known to Java compiler and cannot be used by any other purpose.

Example: [], {}, (), ;, *, =

Square Brackets []:  It is used to defined array element reference.

Parentheses(): These special symbol are used to call the functions and parameters.

Braces{}:  This indicates the starting and ending of the code.

Comma(,): It is used to separate more than parameters, values and statements.

Semicolon(;): It is an operator to use at the end of any statements. It separates the the two statements.


Watch Video-


FAQ – Frequently Asked Questions

Q1. What are the types of Tokens?

There are 5 types of tokens in java. These are as follows-

  1. Keywords
  2. Identifiers
  3. Operators
  4. Literals
  5. Special Symbols

Q2. Is printf a token?

Yes, it is a keyword and keyword comes under token.

Q3. Is 123$abc_ a valid identifier?

No, it is invalid identifier because it is starting with digits.


Summary

In this post, we have covered ‘What is Token in Javaand its type.

Final word, Bookmark this post  ‘What is Token in Java for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

 

Must Read –

Is-A Relationship in Java

Java JVM, JDK and JRE

 

Leave a Comment