Skip to main content

Tokens in java

Tokens in java

Tokens in java Tokens are the various Java program elements which are identified by the compiler. A token is the smallest element of a program that is meaningful to the compiler.






Java tokens can be categorized into the following six types:
a)    Identifiers
b)    keywords  
c)    literals     
d)    operators
e)    separators 
f)       comments

(aIdentifiers – Identifiers refer to the names of variables, functions, arrays etc. given by a programmer. A variable is a name that the program associates with a storage location in memory.

(bJava Keywords – The words which are reserved to do specific tasks at time of designing a programming language is known as Keywords. Following are the JAVA keywords:

abstract
boolean
Break
Cast
Catch
Char
Continue
Default
Do
Extends
Final
Finally
Goto
If
Implements
Instanceof
Int
Interface
Null
Operator
Outer
Rest
Return
Short
This
Throw
Throws
Var
Void
Volatile
Case
Const
Else
New
Public
Synchronized
Byte
Class
Future
Native
Double
Import
Package
Static
While
Try
Transient
Inner

(c)   Literals – A literal is a constant value written into a program instruction. There are four types of literals. These are:
Ø  Numeric Literals – The value of numeric literal can be a positive or negative number. These are four types:

Ø  Integers Literals – Integers Literals are whole numbers and do not have any fractional or decimal part. Example : 3, -8, +4

Ø  Floating point Literals – A floating point literals may be written in two forms called the fractional form. Example: 0.7, 8.9

Ø  Octal Literals – Octal numbers are integer numbers having digits 0 to 7. An octal literal is written by preceding the value with zero. Example: 003, 014, -0127

Ø  Hexadecimal Literals – Hexadecimal numbers are Integer numbers which include  digits 0 to 9 and letters A to F. letter A stands for 10, B for 11, C for 12, D for 13, E for 14 and F stands for 15. A hex literal is normally represented by 0x or 0X. Example: 0x0, 0X3C, 0x15A

Ø  Character Literal – Character literal is either a single alphabet or a single digit, or a single special symbol enclosed within a pair of single quotation mark(‘ ‘). Example: ‘A’, ‘a’,’.’, ‘?’

Ø  Boolean Literals – A Boolean literal can have a value of either “true” or “false”. When we need to represent a condition or state with only two possible values, a boolean should be used.

Ø  String Literals – A string literal is a sequence of alphanumeric characters enclosed in double quotation marks (“ “). Example: “Sum=”, “Result=”

(d)   Operators – An operator operates on one or more variables and performs an action. It consists of words or symbols. Example: +, -, /, *
            The operators can be classified into following categories:
Ø  Arithmetic Operator
Ø  Bitwise Operator
Ø  Relational Operator
Ø  Boolean Logical Operator
Ø  Assignment Operator

(e)     Separators Separators are used to indicate to java compiler the way the items are   grouped in the program code. Example: commas (,), semi-colon (;), colon (:), opening and closing braces ({}) etc.

(f)    Comments – Comments can be included in a program in three different ways in JAVA as given below:
Type
Usages
/* */
All characters between /* and */ are ignored.
//
All characters after the // up to end of the line are ignored.
/** *
Same as /* */, except that the comment can be used to create automatic documentation.

Comments