• Articles
  • Tutorials
  • Interview Questions

Java Tokens - The Basic Building Blocks of Code

Java Tokens

Java tokens are the smallest units or elements of a program. We have six main tokens available in Java:

1. Identifiers

Identifiers are generally defined by users. They are used to name a variable, constant, function, class, or array. Underscores, alphabets, or the dollar sign can be used here as the first character. However, we have to always keep in mind that the identifier name must be different from the reserved keywords.

Let’s have a look at the rules to declare identifiers:

  • Identifiers are always case-sensitive.
  • The first letter of an identifier will always be an alphabet, underscore, or the dollar sign.
  • No white spaces are allowed.

Examples of some valid identifiers:

  1. CarName
  2. PETROL
  3. diameter
  4. i
  5. a1
  6. _carname
  7. $radius
  8. java_array

Example of an invalid identifier: 13diameter
Learn Java

2. Literals

In Java, literals represent a fixed value in the source code. They are defined by a programmer. Literals in Java once defined cannot be changed. There are five main categories of literals:

  • Integer
  • Floating point
  • String
  • Character
  • Boolean
Literal Type of Literal
56 int
45.65 double
false, true boolean
‘S’, ‘4’, ‘-‘ char
“Intellipaat” string
null any reference type
3. Operators

Operators in Java are the symbols that are used to perform operations on operands. Let’s see the total eight types of operators present in Java as listed below:

  • Arithmetic operators
  • Assignment operators
  • Relational operators
  • Logical operators
  • Unary operators
  • Ternary operators
  • Shift operators
  • Bitwise operators
Operators Symbols
Arithmetic + , – , / , * , %
Assignment = , += , -= , *= , /= , %= , ^=
Relational ==, != , < , >, <= , >=
Logical && , ||
Unary ++ , – – , !
Ternary (Condition) ? (Statement1) : (Statement2);
Shift << , >> , >>>
Bitwise & , | , ^ , ~
4. Separators:

Separators are punctuators present in Java. Let’s look at some of the useful separators in Java:

  • Period (.): It is used to separate the package name from the sub-packages and the class. Also, it is used to separate a variable or method from a reference variable.
  • Comma (,): It is basically used to separate two values, statements, and parameters.
  • Parentheses (): We use it to call functions and parse the parameters.
  • Curly braces {}: Whenever we want to start or end a block of code, we use curly braces.
  • Assignment operator (=): We use it to assign a variable and a constant.
  • Semicolon (;): It separates the two statements of the code.
5. Keywords

Keywords are the reserved words in Java. Each of their meaning is already known to the compiler. Let’s look at the Java keywords given in the below table:

case boolean char continue class
abstract catch byte default break
do double else extends final
finally float int if implements
import instanceof for interface long
native new package private protected
public return short static super
switch synchronized this thro throws
transient void try while volatile
assert enum const strictfp goto

Let’s learn about some of these keywords further:

  • case: It is used with the switch statements to mark the blocks of text.
  • catch: It is used to catch the exceptions generated by try statements. We can only use it after a try block.
  • char: It is used to declare a variable that can hold unsigned 16-bit Unicode characters.
  • class: It is used to declare a class.
6. Comments

Comments are used to give a description of the Java code. There are mainly two types of Java comments available as listed below:

  • Block-oriented: It is a multi-line comment that starts with /* and ends with */. It can have multiple rows.
  • Line-oriented: It is a single-line comment and starts with //.

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.