Tokens or Lexical Unit in Java


☞The smallest individual unit in a program is known as token.

☞Tokens used in Java are : Keywords, Identifiers, Literals, Punctuators, and Operators


Identifiers:-

☞Identifiers are programmer-defined names given to the various program elements such as variables, functions, objects, classes, etc.

Examples of legal identifiers: age, _value, __1_value

Examples of illegal identifiers: 123abc, -salary, hello.file

Rules for naming identifiers :-

➺It may contain digits, letters and underscore.

➺It must begin with a letter or underscore but not a digit.

➺Java is case sensitive.


Literals or Constants:-

☞The data items which never change their value throughout the program run.

☞There are several kinds of literals : Integer literals, Floating or Real literals, Character literals, String literals, Boolean literals, Null literal


Integer literals :

➺They are whole numbers without any fractional part.

➺An integer literal must have at least one digit and must not contain any decimal point.

➺It may contain either + or - sign. A number with no sign is assumed as positive.

➺Example : 75, -98, +33, etc


Floating or Real literals:

➺Numbers which are having the fractional part are referred to as floating literals or real literals.

➺It may be a positive or negative number.

➺A number with no sign is assumed to be a positive number.

➺Example : 2.0, 17.5, -0.00256


Character Literals:

➺Any single character enclosed within single quotes is a character literal.

➺Example : 'A', '3', ' ', '#'

Note : '3.4', '-4' etc are not a character literal because there is more than one character.


String Literals:

➺It is a sequence of characters surrounded by double-quotes within a limit of 256 characters.

➺Example : "abc", "23", "1-h-4j-i"


Boolean Literal:

➺It is true or false.


Null Literal:

➺It denotes the absence of value.

➺It is used to initialize an object or array.

➺Example : Integer n = null;


Keywords :-

☞Keywords are reserved words that convey a special meaning to the compiler.

Example :- if, else, default, switch, case, break, continue, do, while, for, true, false, new, null, package, private, public, byte, short, int, long, char, float, double, this, void, return, import, boolean, class etc.

Note : Keywords are always written in small letter.

Punctuators :-

☞Punctuators are symbols that are used in programming languages to organize sentence structures, and indicate the rhythm and emphasis of expressions, statements, and program structure.

☞The following characters are used as punctuators:

[ ] Brackets These indicates single and multidimensional array subscripts
() Parenthesis These indicate function calls and function parameters.
{ } Braces Indicate the start and end of compound statements.
; Semicolon This is a statement terminator.
, Comma It is used as a separator.
: Colon It indicates a labeled statement
= Equal to It is used as an assigning operator.

Operators:-

☞An operator is a symbol or character which triggers some operation (computation) on its operands.

☞Types of operators:- Unary Operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Conditional Operator, Assignment Operators,