Tokens
Token is the smallest individual unit or element of a program. It is the building block used to write the code. The Java compiler breaks the source code into these tokens during the compilation process to understand and process the code.
Types of Tokens in Java
Java programs consist of the following types of tokens
Keywords
Identifiers
Literals
Operators
Comments
Keywords
Keywords are the predefined words in the programming language which will have some kind of predefined meaning.
Examples: class, public, static, void, int, etc.
Rule: All keywords should be written in lowercase.
Identifiers
Identifiers are the programmers defined words the words for which the programmer will give some kind of meaning. are called as identifiers.
Example: class name, variable name, function name, interface name, package name, etc.
Rules:
These can be alphanumeric.
No special character allowed apart from $, &.
keywords can’t be identifiers.
Because it is alphanumeric it can’t start with no, it either starts with alphabet, _, &.
Literals
literals are values used in the program
There are 4 types of literals
Numeric literals
character literals
String literals
Boolean literals
Numeric literals
Any numeric values considered numeric literals
Example: 10, 10.5, etc.
Character literals
Anything which is enclosed within single quotes is called character as character literals. and we should enclose only one character within the single quote.
Example: ‘A’, ‘g’, ‘9’ etc.
String literals
Anything which is enclosed within double quotes is considered as string literals.
Example: “Hello”, “1000”, “g” etc.
Boolean literals
there are only 2 boolean literals.
true
false
Comments
Comments in programming are notes written by the developer within the code that explain what the code is doing, clarify complex parts of the code, or provide additional context.
These notes are ignored by the computer when the code runs, meaning they do not affect the program's behavior. Comments are primarily used to make the code more readable and understandable for humans.
Types of Comments in Java:
Single-line comments (
//
): Used for short explanations on a single line.// This is a comment
Multi-line comments (
/* */
): Used for longer explanations or blocking out multiple lines of code./* This is a multi-line comment that spans across multiple lines. */
Javadoc comments (
/** */
): Special comments used to document classes and methods, usually for creating external documentation./** * This method adds two numbers. * @param a First number * @param b Second number * @return Sum of a and b */ public int add(int a, int b) { return a + b; }
Conclusion
After learning about Tokens, Variables, Identifiers, Literals, and Operators in Java, you’ve covered some of the basic building blocks needed to write a program.
Tokens are the smallest pieces of a program, including keywords, operators, and identifiers.
Variables store data, and identifiers are the names you give to things like variables and methods.
Literals represent fixed values like numbers or text, while operators allow you to perform actions on these values, like adding or comparing them.