Java Tokens

Java Tokens

Java Tokens

Java Tokens

Java Tokens

Java Tokens


Java Tokens

Java tokens

When we learn a language like English, we learn the parts of speech. These parts of speech are:- nouns, pronouns, adjectives, verbs, etc. Just like that, we have Java tokens for Java Programming Language. Java tokens serve as the essential building blocks that compose a Java program. Java tokens are categorized into five types:- 


  • Keywords
  • Identifiers
  • Literals
  • Operators
  • Separators


Developers may use these tokens to generate Java programmes’ fundamental words and sentences. We will explore each type of Java token in this blog article. With real life examples let’s understand their significance & usage.


Keywords in Java

The Keywords are like special words in Java that have specific meanings to the compiler. They are reserved and cannot be used as names for anything (variables, methods, or classes). They are like  “reserved words” in a language.


For example:- Let’s take the keyword “public”. It tells the compiler that we can access a class / method from anywhere in the code. It’s like the chef saying, “This recipe is public! Anyone can use it & share it with others”. It’s a way of controlling the visibility and accessibility of certain parts of the code.


So, you use keywords in a Java program to define the structure & behavior of your code. Just like following a recipe with special words and giving unique names to ingredients and steps, using keywords in Java helps the computer understand and execute your code correctly.


Here are some examples of keywords

abstract a class cannot be instantiated.
assert insert assertions into a program.
boolean a variable can only hold the value true or false.
break exit a loop or switch statement.
byte a variable can only hold a whole number between -128 and 127.
case label a block of code in a switch statement.
catch handle exceptions.
char a variable can only hold a single character.
class a new class is being defined.
constNot currently used.
continue skip the rest of the current iteration of a loop.
default specify a default case in a switch statement.
do create a do-while loop.
double a variable can hold a floating-point number with 64 bits of precision.
else specify an alternative block of code to execute if a condition is not met.
enum a new enumeration is being defined.
extends specify that a class is derived from another class.
final a variable cannot be changed after it is initialized.
finally execute code regardless of whether an exception is thrown.
float a variable can hold a floating-point number with 32 bits of precision.
for create a for loop.
gotoNot currently used.
if create an if statement.
implements specify that a class implements an interface.
import import a class or package into a program.
instanceof test if the object is an instance of a class.
intwhole number between -2147483648 and 2147483647.
interface a new interface is being defined.
longwhole number between -9223372036854775808 and 9223372036854775807.
nativeNot currently used.
new create a new object.
package declare the package that a class belongs to.
private a variable or method can only be accessed by code within the class in which it is defined.
protected can only be accessed by the code in the class
public variable or method can be accessed anywhere code.
return exit a function and return a value.
short a variable can hold a whole number between -32768 and 32767.
static a variable or method is associated with the class rather than an instance of the class.
strictfp floating-point operations should be performed with strict precision.
switch create a switch statement.
synchronized control access to a shared resource.
this Refers to a current object
throw throw an exception.
throws specifies that a method can throw an exception.
transient a variable should not be serialized when an object is serialized.
try create a try-catch block.
void a method does not return a value.
volatile a variable may be changed by another thread.
while create a while loop.


Here are some examples of how keywords can be used in a Java program:



The code above defines a class called “MyClass”. The class has two methods, “myMethod()” and “myOtherMethod()”. 

The “myMethod()” method is “static”, which means it can be accessed without creating an instance of the class. The “myOtherMethod()” method is not static, which means it must be accessed by creating an instance of the class. 

The code also declares a variable called “myVariable”. The variable is of type “int”, which means it can store an integer value. The code then prints the value of “myVariable” to the standard output stream.


Identifiers in Java 

In a Java program, an identifier is like a name we give to something. It could be a variable, method, class, or package. Identifiers help us refer to specific things in our code.


Imagine you’re organizing a collection of books. Each book needs a unique identifier, like a title or a code. That makes it easy to find them. In Java, identifiers do the same job—they give unique names to different parts of your program.


Imagine you’re writing a Java program to calculate the area of a rectangle. In this program, we’ll use identifiers to name the variables and methods.



In this code, we have several identifiers. Let’s break them down:-


  • `length` and `width` are identifiers for variables. They are meaningful names that describe what they represent.
  • `area` is an identifier for a variable that will store the calculated area of the rectangle. It represents the result of the calculation.
  • `calculateArea` is an identifier for a method that calculates the area of the rectangle using those values. It returns the calculated area.


By using these identifiers, the code becomes more readable. When someone reads the code, they can easily understand the purpose of each variable and method based on their descriptive names.



In Java, a literal is a constant value that appears directly in the source code. Literals can be used to represent numbers, strings, characters & boolean values.

Here are some examples of literals:

  • Integer literals: Integer literals represent whole numbers. We can write them as..
    • Decimal: 123
    • Octal: 0123
    • Hexadecimal: 0x123
  • Floating-point literals: Floating-point literals represent numbers with decimal points. They can be written in decimal or scientific notation.
    • Decimal: 123.45
    • Scientific notation: 1.2345e+02
  • String literals: String literals represent sequences of characters. They are enclosed in double quotes.
    • “Hello, world!”
  • Character literals: Character literals represent a single character. They are enclosed in single quotes.
    • ‘a’
  • Boolean literals: Boolean literals represent the values true and false. They are written as true and false, respectively.
    • true
    • false

Literals can be used to initialize variables, to pass arguments to methods, and to perform calculations.

Here are some examples of how literals can be used:



Table of literals in Java:


Literal typeDescriptionExamples
Integer literalRepresents a whole number.123, 0123, 0x123
Floating-point literalRepresents a number with a decimal point.123.45, 1.2345e+02
String literalRepresents a sequence of characters.“Hello, world!”
Character literalRepresents a single character.‘a’
Boolean literalRepresents the values true and false.true, false



The Operators are a feature in Java. Operators are symbols or keywords that perform specific operations on operands (values, variables, or expressions). They are used to manipulate and operate on data.

Look at the following table.


Table of operators in Java

ArithmeticPerform mathematical calculations+, -, *, /, %int result = 10 + 5;
AssignmentAssign a value to a variable= += -= *= /= %=x = 10; x += 5;
ComparisonCompare two values for equality or order== != < > <= >=if (x > y)
LogicalPerform logical operations&& `
BitwisePerform operations at the bit level& `^ ~ << >> >>>`
ConditionalExecute different actions based on a condition? :int max = (x > y) ? x : y;
Increment/DecrementIncrease or decrease a value by 1++ —i++; j–;
TernaryShort form of conditional statementscondition ? value1 : value2int max = (x > y) ? x : y;
instanceofChecks if an object belongs to a classinstanceofif (obj instanceof MyClass)
Type castingConvert a value from one type to another(type) valueint x = (int) 3.14;


These are some of the commonly used operators in Java. Each operator has a specific purpose. We can use them to perform different operations on operands. By understanding and utilizing these operators effectively, you can manipulate data and perform calculations in your Java programs.


In Java, separators are characters used to separate elements within the code. They help the compiler understand the structure and syntax of the program. Here are some commonly used separators in Java:-


Semicolon (;)

 The semicolon is used to separate statements in Java. It indicates the end of a statement. Because of this, we can write multiple statements on one line.



Comma (,)

The comma is used to separate items in a list, such as arguments in a method call or elements in an array.



Dot (.) and Arrow (-)

These separators are used to access members of a class or object. The “dot (.)” operator is used for accessing fields, methods, and nested classes. The “arrow (->)” operator is used for accessing members of objects in lambda expressions.



Parentheses ( )

Parentheses are used for grouping expressions, passing arguments to methods, and defining method signatures.




These separators are essential for writing correct and readable Java code. Understanding their usage and proper placement is crucial for the compilation and execution of your programs.

Table of separators in Java

Semicolon (;)Terminates x = 10;
Comma (,)Separates multiple values in a[] numbers = {10, 20, 30};
Dot (.)Accesses the properties or methods of an object.String str = “Hello, world!”; int length = str.length();
Colon (:)Defines a block of code.public void myFunction() { // body of the function }


How Java Tokens work

As we have seen earlier, Java tokens are the fundamental units of a Java program. They are the smallest elements that make up the source code. When you write a Java program, the compiler breaks it down in tokens. Then it analyzes and processes the program.

Here’s how Java tokens work:


The process starts with tokenization, where the compiler divides the source code into individual tokens. It scans the code character by character and identifies different types of tokens.

Types of Tokens

There are five main types of Java tokens:

  • Keywords: Reserved words in Java with predefined meanings. Examples: public, class, static.
  • Identifiers: Names used for variables, methods, classes, etc. Examples: myVariable, myMethod.
  • Literals: Constant values used in the code. Examples: 42 (integer literal), 3.14 (floating-point literal), “Hello” (string literal).
  • Operators: perform specific operations. Examples: +, -, *, /.
  • Separators: used to separate tokens. Examples: (, ), {, ;.

Syntax Analysis

After tokenization, the compiler performs a syntax analysis, also known as parsing. It examines the arrangement and structure of the tokens to ensure they follow the rules of the Java language.

Semantic Analysis

The compiler then performs semantic analysis, where it checks if the tokens and their combinations are meaningful and confirms the rules of Java. It verifies variable types, method signatures, and other semantic aspects.

Intermediate Representation

The compiler generates an intermediate representation, such as an Abstract Syntax Tree (AST), which represents the structure and relationships between the tokens in a more organized manner.

Code Generation

Finally, the compiler translates the processed tokens into executable machine code or “bytecode”. Then the JVM (Java Virtual Machine) can execute this bytecode.

By breaking down the source code into tokens and analyzing their structure and meaning, the compiler can understand the code and perform various checks and transformations to produce a runnable program.


In conclusion, Java tokens are like the LEGO pieces of a Java program. They come in different types like keywords, identifiers, literals, operators, and separators. 

When we write code, the compiler examines these tokens to understand our instructions. It’s like a puzzle where the compiler puts the pieces together to make sense of our program. 

By using the right tokens in the right way, we can create working and understandable Java programs.


Technogeeks is an excellent learning platform for Learning Core and Advanced Java. Enroll to kickstart your journey as a Java developer.


Checkout our courses on the Java programing below


👉 👈


👉 👈



Leave a Reply

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

Blogs You May Like

Get in touch to claim Best Available Discounts.

Looking for Job Assistance or Placement Support?

× How can I help you?