• Articles
  • Tutorials
  • Interview Questions
  • Webinars

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
  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
Learn Python from an expert and kick-start your career in Python. Enroll in our Python Course
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 & , | , ^ , ~

Want to ace your next Java interview? Check out our recent blog post about the most common Java interview questions and answers!

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.

Learn how to use threads in Java to improve performance. Start your Multithreading in Java journey today!

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.

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 20 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 27 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 03 Aug 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Senior Consultant Analytics & Data Science

Presenting Sahil Mattoo, a Senior Consultant Analytics & Data Science at Eli Lilly and Company is an accomplished professional with 14 years of experience across data science, analytics, and technical leadership domains, demonstrates a remarkable ability to drive business insights. Sahil holds a Post Graduate Program in Business Analytics and Business Intelligence from Great Lakes Institute of Management.