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 the Java
Programming Language. Java tokens serve as the essential building blocks that compose a Java
program.
Tokens
Tokens are the smallest unit of any programming language.
Tokens are classified into the following types:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Comments
6. Separators
Developers may use these tokens to generate Java programmer’s fundamental words and sentences.
We will explore each type of Java token in this blog article.
Keywords
Keywords are predefined and reserved words of any programming language. In Java, keywords are
written in lowercase. The Keywords are like special words in Java that have specific meanings to
the compiler.
Here are some examples of keywords:
Press enter or click to view image in full size
Note: true, false, and null look like keywords, but in actuality they are literals.
However, they still can’t be used as identifiers in a program.
Identifiers
Identifiers, it is a name given by programmers to elements of java. Identifiers help us refer to
specific things in our code.
Assume that you are arranging a library of books. Every book requires a special title, such as a code
or title. That helps identify them. Identifiers in Java perform the similar role of giving different
names to elements of code or program.
Here are some examples of identifiers:
→ class name
→ variable name
→ method name
→ interface name
→ package name
Literals
Literals are the different types of (constant) values we using in java programming language.
Literals are classified into the following types:
1. Numbers → (Integer and Float)
2. Characters
3. String
4. Boolean
Here are some examples of literals:
1. (a) Integer: Integer literals represent whole numbers.
Ex: 123 (Whole), 0123 (Octal) , 0x123 (Hexadecimal), etc.
2. (b) Float: Floating-point literals represent numbers with decimal points. They can be
written in decimal or scientific notation.
Ex: 123.45 (Decimal), 1.2345e+02 (scientific notation), etc.
3. Characters: Character literals represent a single character. They are enclosed in
single quotes (‘char’).
Ex: ‘A’, ‘a’, ‘$’, ‘@’, etc.
4. String: String literals represent sequences of characters. They are enclosed in double
quotes (“ String”).
Ex: “Java”, “Programming”, etc.
5. Boolean: Boolean literals represent the values true and false. They are written as true
and false, respectively.
Ex: Either true or false.
Literal in tabular format:
Press enter or click to view image in full size
Operators
With the help of operators, we can perform a particular task. Operators are symbols or keywords
that perform specific operations on operands (values, variables, or expressions). They are used to
manipulate and operate on data.
Operators are classified into three types are as follows:
1. Unary Operator
2. Binary Operator
3. Ternary Operator
These are a few of the Java operators that are often used. Every operator has a specific purpose.
They can be applied to operands in a variety of ways. You may alter data and do computations in
your Java applications by learning how to use these operators.
We will discuss later each of the operators in the operators’ blog separately. For now, just take a
look and remember it.
Comments
The comments are a meaningful way to deliver messages. It is used to add information about the
code, warnings or suggestions. So that a user can easily interpret the code. The comments are
ignored by the compiler.
Advantages of Comments:
1. To make code easy to understand
2. To avoid the unnecessary code
Types of Comments:
1. Single Line Comment
Ex: // This is a single-line comment
//Single Line Comment
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window - single line comment
[Link]("Hello, World");
}
}
2. Multi-Line Comments
Ex: /* Multi-Line Comments*/
//Multi-Line Comment
public class MyCalculator {
public static void main(String[] args) {
/*
This program demonstrates a simple calculator
that performs addition, subtraction, multiplication,
and division operations on two numbers.
*/
int num1 = 10;
int num2 = 5;
// Addition
int sum = num1 + num2;
[Link]("Sum: " + sum);
}
}
Table of Comments in Java:
Press enter or click to view image in full size
Separators
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:
1. Semicolon (;)
2. Comma (,)
3. Dot (.) and Arrow (-)
4. Parentheses( )
Table of separators in Java:
Press enter or click to view image in full size
It takes these separators to write proper, understandable Java code. Compiling and running your
programs depend heavily on your ability to use them appropriately.
Happy Java Learning: Regard Manvendra Sir