0% found this document useful (0 votes)
25 views6 pages

JAVA Tokens

Java tokens are the smallest elements of a Java program, identified by the compiler, and include keywords, identifiers, literals, operators, separators, and comments. Tokens are separated by delimiters and are essential for the compilation process, as they help in detecting errors. Understanding the types of tokens and their functions is crucial for writing effective Java code.

Uploaded by

sharmila.s
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)
25 views6 pages

JAVA Tokens

Java tokens are the smallest elements of a Java program, identified by the compiler, and include keywords, identifiers, literals, operators, separators, and comments. Tokens are separated by delimiters and are essential for the compilation process, as they help in detecting errors. Understanding the types of tokens and their functions is crucial for writing effective Java code.

Uploaded by

sharmila.s
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
You are on page 1/ 6

Java Tokens

In Java, the program contains classes and methods. Further, the methods
contain the expressions and statements required to perform a specific
operation. These statements and expressions are made up of tokens. In
other words, we can say that the expression and statement is a set
of tokens. The tokens are the small building blocks of a Java program that
are meaningful to the Java compiler. Further, these two components
contain variables, constants, and operators. In this section, we will
discuss what is tokens in Java.

What is token in Java?


The Java compiler breaks the line of code into text (words) is called Java
tokens. These are the smallest element of the Java program. The Java
compiler identified these words as tokens. These tokens are separated by
the delimiters. It is useful for compilers to detect errors. Remember that
the delimiters are not part of the Java tokens.

1. token <= identifier | keyword | separator | operator | literal | comme


nt
For example, consider the following code.

1. public class Demo


2. {
3. public static void main(String args[])
4. {
5. System.out.println("javatpoint");
6. }
7. }
In the above code snippet, public, class, Demo, {, static, void, main,
(, String, args, [, ], ), System, ., out, println, javatpoint, etc. are the
Java tokens.

The Java compiler translates these tokens into Java bytecode. Further,
these bytecodes are executed inside the interpreted Java environment.

Types of Tokens
Java token includes the following:

o Keywords
o Identifiers
o Literals
o Operators
o Separators
o Comments

Keywords: These are the pre-defined reserved words of any


programming language. Each keyword has a special meaning. It is always
written in lower case. Java provides the following keywords:

w
01. 02. boolean 03. byte 04. break 05. class
abstract

06. case 07. catch 08. char 09. 10. default


continue

11. do 12. double 13. else 14. 15. final


extends

16. finally 17. float 18. for 19. if 20.


implements

21. import 22. instanceof 23. int 24. 25. long


interface

26. native 27. new 28. 29. private 30. protected


package

31. public 32. return 33. short 34. static 35. super

36. switch 37. 38. this 39. thro 40. throws


synchronized

41. 42. try 43. void 44. volatile 45. while


transient

46. assert 47. const 48. enum 49. goto 50. strictfp

Identifier: Identifiers are used to name a variable, constant, function,


class, and array. It usually defined by the user. It uses letters,
underscores, or a dollar sign as the first character. The label is also known
as a special kind of identifier that is used in the goto statement.
Remember that the identifier name must be different from the reserved
keywords. There are some rules to declare identifiers are:

o The first letter of an identifier must be a letter, underscore or a


dollar sign. It cannot start with digits but may contain digits.
o The whitespace cannot be included in the identifier.
o Identifiers are case sensitive.

Some valid identifiers are:

1. PhoneNumber
2.
3. PRICE
4. radius
5. a
6. a1
7. _phonenumber
8. $circumference
9. jagged_array
10. 12radius //invalid
Literals: In programming literal is a notation that represents a fixed value
(constant) in the source code. It can be categorized as an integer literal,
string literal, Boolean literal, etc. It is defined by the programmer. Once it
has been defined cannot be changed. Java provides five types of literals
are as follows:

o Integer
o Floating Point
o Character
o String
o Boolean

Literal Type

23 int

9.86 double

false, true boolean


'K', '7', '-' char

"javatpoint" String

Null any reference type

Operators: In programming, operators are the special symbol that tells


the compiler to perform a special operation. Java provides different types
of operators that can be classified according to the functionality they
provide. There are eight types of operators in Java, are as follows:

o Arithmetic Operators
o Assignment Operators
o Relational Operators
o Unary Operators
o Logical Operators
o Ternary Operators
o Bitwise Operators
o Shift Operators

Operator Symbols

Arithmetic +,-,/,*,%

Unary ++ , - - , !

Assignment = , += , -= , *= , /= , %= , ^=

Relational ==, != , < , >, <= , >=

Logical && , ||

Ternary (Condition) ? (Statement1) : (Statement2);

Bitwise &,|,^,~

Shift << , >> , >>>

Separators: The separators in Java is also known as punctuators. There


are nine separators in Java, are as follows:

1. separator <= ; | , | . | ( | ) | { | } | [ | ]
Note that the first three separators (; , and .) are tokens that separate other tokens, and
the last six (3 pairs of braces) separators are also known as delimiters. For example,
Math.pow(9, 3); contains nine tokens.

o Square Brackets []: It is used to define array elements. A pair of


square brackets represents the single-dimensional array, two pairs
of square brackets represent the two-dimensional array.
o Parentheses (): It is used to call the functions and parsing the
parameters.
o Curly Braces {}: The curly braces denote the starting and ending
of a code block.
o Comma (,): It is used to separate two values, statements, and
parameters.
o Assignment Operator (=): It is used to assign a variable and
constant.
o Semicolon (;): It is the symbol that can be found at end of the
statements. It separates the two statements.
o Period (.): It separates the package name form the sub-packages
and class. It also separates a variable or method from a reference
variable.

Comments: Comments allow us to specify information about the program


inside our Java code. Java compiler recognizes these comments as tokens
but excludes it form further processing. The Java compiler treats
comments as whitespaces. Java provides the following two types of
comments:

o Line Oriented: It begins with a pair of forwarding slashes (//).


o Block-Oriented: It begins with /* and continues until it founds */.

You might also like