0% found this document useful (0 votes)
21 views4 pages

Understanding Java Tokens

In Java, tokens are the smallest meaningful elements of a program, classified into keywords, identifiers, constants/literals, operators, and separators. Keywords are reserved words with specific functions, while identifiers are user-defined names for variables and functions. Constants are fixed values that cannot be modified, and operators perform various operations, with separators indicating the end of statements.

Uploaded by

mmp.scos
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Understanding Java Tokens

In Java, tokens are the smallest meaningful elements of a program, classified into keywords, identifiers, constants/literals, operators, and separators. Keywords are reserved words with specific functions, while identifiers are user-defined names for variables and functions. Constants are fixed values that cannot be modified, and operators perform various operations, with separators indicating the end of statements.

Uploaded by

mmp.scos
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Tokens

In Java, Tokens are the smallest elements of a program that is meaningful to the
compiler. They are also known as the fundamental building blocks of the program.
Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants/Literals
4. Operators
5. Separators
1. Keyword
Keywords are pre-defined or reserved words in a programming language. Each
keyword is meant to perform a specific function in a program. Since keywords are
referred names for a compiler, they can’t be used as variable names because by
doing so, we are trying to assign a new meaning to the keyword which is not
allowed. Java language supports the 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
2. Identifiers
Identifiers are used as the general terminology for naming of variables, functions
and arrays. These are user-defined names consisting of an arbitrarily long sequence
of letters and digits with either a letter or the underscore (_) as a first character.
Identifier names must differ in spelling and case from any keywords. You cannot use
keywords as identifiers; they are reserved for special use. Once declared, you can
use the identifier in later program statements to refer to the associated value. A
special kind of identifier, called a statement label, can be used in goto
statements. Examples of valid identifiers:
MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
_myvariable
$myvariable
sum_of_array
geeks123
Examples of invalid identifiers:
My Variable // contains a space
123geeks // Begins with a digit
a+c // plus sign is not an alphanumeric character
variable-2 // hyphen is not an alphanumeric character
sum_&_difference // ampersand is not an alphanumeric character
3. Constants/Literals
Constants are also like normal variables. But the only difference is, their values
cannot be modified by the program once they are defined. Constants refer to fixed
values. They are also called as literals. Constants may belong to any of the data
type. Syntax:
final data_type variable_name;
1
import [Link].*;
2

3
class GFG {
4
public static void main (String[] args) {
5
6
// Here final keyword is used
7
// to define the constant PI
8
final double PI = 3.14; // Use double instead of int
9

10
// Example usage of PI
11
[Link]("The value of PI is: " + PI);
12
}
13
}

Output
The value of PI is: 3.14
4. Operators
Java provides many types of operators which can be used according to the need.
They are classified based on the functionality they provide. Some of the types are-
 Arithmetic Operators
 Unary Operators
 Assignment Operator
 Relational Operators
 Logical Operators
 Ternary Operator
 Bitwise Operators
 Shift Operators
 instance of operator
 Precedence and Associativity
5. Separators
Separators are used to separate different parts of the codes. It tells the compiler
about completion of a statement in the program. The most commonly and
frequently used separator in java is semicolon (;).
int variable; //here the semicolon (;) ends the declaration of the variable
1
import [Link].*;
2

3
class GFG {
4
public static void main (String[] args) {
5

6
//Here the semicolon (;) used to
7
// end the print statement
8
[Link]("GFG!");
9
}
10
}

Common questions

Powered by AI

Logical operators in Java, such as `&&` (AND), `||` (OR), and `!` (NOT), are used to form compound boolean expressions, allowing decisions based on multiple conditions. They are frequently used in control flow statements like `if`, `while`, and `for` loops, where a program executes code blocks based on the true or false evaluation of combined conditions. For example, in the statement `if (x > 5 && y < 10)`, both conditions involving `x` and `y` must be true for the block to execute .

Identifiers in Java are names given to elements like variables, methods, classes, etc., created by the programmer. They are different from keywords as they are not reserved words and can be customized by the user to represent data elements. The rules for naming identifiers include starting with a letter or an underscore, followed by a combination of letters and digits. Identifiers must not use keywords and should be distinct in spelling and case from any keywords .

The `instanceof` operator in Java is used for type checking, determining if an object is an instance of a specific class or subclass. It returns a boolean value, true if the object is of the specified type, otherwise false. For example, `if (animal instanceof Dog)` checks if `animal` is a `Dog` object. Relational operators, like `<`, `>`, `<=`, and `>=`, are used to compare numerical values, resulting in boolean evaluations. They are commonly used in conditions and loops to direct program flow. The `instanceof` operator is typically applied in type-safe casts and polymorphism, whereas relational operators are utilized in arithmetic comparisons .

Assignment operators in Java are used to assign values to variables. The fundamental assignment operator is `=`, which assigns the value on the right to the variable on the left. Assignment operators can also combine other operations, such as `+=`, which adds a value to the current variable and updates it. For example, `int a = 5; a += 2;` assigns the value 5 to `a`, and then updates `a` to be 7 with the `+=` operator. These operators are critical for managing and manipulating data within programs .

Operator precedence in Java dictates the order in which operations are performed in expressions, similar to mathematical precedence rules. Higher precedence operators are evaluated before lower precedence ones. For example, in the expression `3 + 4 * 2`, the multiplication operator `*` has higher precedence than addition `+`, so it is evaluated first, resulting in `3 + (4 * 2) = 11`. Precedence rules impact how expressions are parsed and understood by the compiler, emphasizing the importance of parentheses to override natural precedence when necessary .

Constants in Java, also known as literals, refer to fixed values that cannot be altered during program execution. They are declared using the `final` keyword. Regular variables, on the other hand, can be modified throughout the program. For example, using the code `final double PI = 3.14;`, `PI` is a constant whose value cannot change, whereas a regular variable like `double radius;` could have its value altered by the program .

Shift operators in Java, such as `<<` (left shift), `>>` (right shift), and `>>>` (unsigned right shift), manipulate bit patterns within integer data types. The left shift operator `<<` shifts bits to the left and fills zeroes on the right, while `>>` shifts bits to the right, preserving the sign bit for negative numbers. `>>>` performs a right shift without sign extension. These operators are commonly used in tasks such as bit manipulation algorithms, low-level device interfacing, and optimizing mathematical operations on large numbers .

Keywords in Java are predefined or reserved words that have specific meanings to the compiler. They are integral to the structure of Java programs as they perform specific functions and define the syntax and control flow. Because they are reserved for these special purposes, they cannot be used as identifiers or variable names, as this would lead to ambiguity in the code and could potentially alter the intended function of the keywords .

Separators in Java serve to separate different pieces of code and elements within the program, aiding in structuring and clarity. The most common separator is the semicolon (;), which is used to terminate statements and clarify expression boundaries, making the code understandable by the compiler. For example, `int a = 5;` uses a semicolon to mark the end of the declaration. Braces {} and parentheses () also act as separators by defining blocks of code and parameter lists, respectively .

Unary operators in Java, such as `++` (increment) or `--` (decrement), are preferred over arithmetic operators when a variable needs to be increased or decreased by exactly one. Unary operators offer concise syntax and are commonly used in loops for iterating counters. For example, `i++` within a `for` loop is a succinct way to increment `i` by 1 each iteration, enhancing code readability compared to `i = i + 1`, although functionally equivalent. Unary operators are especially favored in performance-sensitive code due to minimal syntax overhead .

You might also like