What Are Tokens In Java?
In Java, tokens are the basic building blocks of a program. They are the smallest individual units of a program that have meaning to the compiler and are used to represent the various elements of a program, such as keywords, identifiers, operators, and literals. Here are the different types of java tokens that can be found:Keywords:
These are reserved words that have a specific meaning in the Java language. Examples of keywords in Java include class, public, private, if, else, while, for, switch, case, break, continue, return, and static, among others. In Java, a keyword is a reserved word that has a specific meaning and purpose within the language. Keywords are used to define various constructs, such as control flow statements, data types, access modifiers, and more. As a token in Java, a keyword is a sequence of characters that are treated as a single unit by the Java compiler. When Java code is compiled, the compiler recognizes keywords as special words with a predefined meaning and function within the language. Examples of Java keywords include:- public, private, protected: access modifiers used to control access to class members.
- class, interface, enum: used to define different types of Java classes.
- if, else, switch, while, do, for: used to define control flow statements.
- int, float, boolean, char: used to define data types.
Identifiers:
These are names that are used to identify variables, methods, classes, and other elements of a program. An identifier must start with a letter or underscore and can contain letters, digits, and underscores. Identifiers are case-sensitive in Java. In Java, an identifier is a sequence of characters used to name a variable, method, class, or another program element. Identifiers are used to refer to these program elements throughout the code, allowing programmers to access and manipulate them.An identifier in Java must follow certain rules:
- It can only contain letters, digits, underscores (_), and dollar signs ($).
- It must begin with a letter, an underscore (_), or a dollar sign ($).
- It cannot be a Java keyword.
- myVariable
- _myVariable
- $myVariable
- MyClass
- myMethod
Operators:
These are symbols or token bus that are used to perform operations on operands, such as arithmetic operations (+, -, *, /, %), logical operations (&&, ||, !), relational operations (==, !=, <,>, <=,>=), and assignment operations (=, +=, -=, *=, /=, %=, etc.). In Java, an operator is a symbol that represents a specific action or computation that can be performed on one or more operands. Operators are used to manipulate and compare data values in Java programs.Java operators can be classified into several categories based on their functionality:
- Arithmetic Operators: Used to perform arithmetic operations such as addition, subtraction, multiplication, division, and modulo. Examples: +, -, *, /, %.
- Relational Operators: Used to compare values and determine the relationship between them. Examples: ==, !=, <,>, <=,>=.=,>,>
- Logical Operators: Used to perform logical operations on boolean values. Examples: && (logical AND), || (logical OR), ! (logical NOT).
- Bitwise Operators: Used to perform bitwise operations on binary values. Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift), >>> (unsigned right shift).
- Assignment Operators: Used to assign values to variables. Examples: =, +=, -=.
Separators:
These are symbols that are used to separate different parts of a program, such as commas (,), semicolons (;), parentheses (()), braces ({ }), and brackets ([]). In Java, separators are characters used to separate or group parts of a Java program. Separators are not operators and do not perform any operations on data values.Java has several types of separators:
- Semicolons (;): Used to separate statements in Java. Every statement in Java must end with a semicolon.
- Commas (,): Used to separate multiple arguments or parameters in method calls or definitions or to separate multiple variable declarations.
- Parentheses (( and )): Used to group expressions, define method parameters, and control the order of evaluation.
- Braces ({ and }): Used to define a block of code, such as a method body or a class definition.
- Brackets ([ and ]): Used to define arrays or to access individual elements of an array.
- Periods (.): Used to access class members or invoke methods on objects.
- Colon (:): Used in switch statements and for-each loops to separate case values or to define iteration variables.
Literals:
These are values that are directly specified in the program, such as integer literals (e.g., 42), floating-point literals (e.g., 3.14), boolean literals (true or false), character literals (e.g., ‘a’), and string literals (e.g., “Hello, world!”). In Java, a literal is a notation used to represent a fixed value in the source code. Literals are used to represent values of data types such as integers, floating-point numbers, characters, and strings.Java supports several types of literals:
- Integer literals: Used to represent integer values, such as 42 or 0xFF.
- Floating-point literals: Used to represent floating-point values, such as 3.14159 or 1.0e-6.
- Boolean literals: Used to represent boolean values, such as true or false.
- Character literals: Used to represent single characters, enclosed in single quotes, such as ‘a’ or ‘9’.
- String literals: Used to represent sequences of characters enclosed in double quotes, such as “Hello, World!”.
- Null literal: Used to represent a reference that does not refer to any object, represented by the keyword null.