You are on page 1of 11

Java Tokens

• Java program is collection of tokens, comments and white spaces.

• Smallest individual units in a program is known as tokens.


– Smallest units of java language are the characters used to write java tokens.
– These characters are defined by the Unicode character set
– Unicode is a 16-bit character coding system and currently supports more than
34,000 defined characters derived from 24 languages from America, Europe,
middle east, Africa, Asia and India,
– ASCII Character set is subset of Unicode which include letters, digits and
punctuation marks used in normal English.

• Compiler recognizes them for building up expressions and statements.

• Five types of tokens


– Reserved keywords
– Identifiers
– Literals
– Operators
– separators
• Elements of java class
• Keywords
– Essential and implement specific features of the
language.
– Java language has reserved 50 words as keywords
– These keywords combined with operators and
separators according to a syntax, form definition
of the java language.
– Keywords cannot be used as names for variables,
classes, methods and so on.
– All keywords to be written in lower case letters.
– One can use these words as identifiers by
changing one or more letters to uppercase (bad
practice)
• Identifiers
– are programmer designed tokens.
– They are used for naming class, methods, variables,
objects, labels, packages and interfaces in a program.
– Java identifiers follow the following rules
• They can have alphabets, digits, and the underscore and
dollar sign characters.
• They must not begin with a digit
• Uppercase and lowercase letters are distinct
• They can be of any length
– Identifiers must be meaningful, short enough to be
quickly and easily typed and long enough to be
descriptive and easily read.
• Java developers have followed some naming conventions.
– Names of all public methods and instance variable start with a leading
lower case letter.
• Examples : average, sum

– When more than one words are used in a name, the second and
subsequent words are marked with a leading uppercase letters.
• Examples: dayTemperature, firstDayOfMonth, totalMarks

– All private and local variables use only lowercase letters combined
with underscores
• Examples: length, batch_strength

– All classes and interfaces start with a leading uppercase letter and
each subsequent word with a leading upper case letter
• Examples: Student, HelloJava, Vehicle, MotorCycle

– Variables that represent constant values use all uppercase letters and
underscores between words.
• Examples: TOTAL, F_MAX, PRINCIPAL_AMOUNT
• Literals
– Literals in java are a sequence of characters (digits,
letters, and other characters) that represent
constant values to be stored in variables. Java
language specifies 5 major types of literals.
• Integer literals
• Floating _point literals
• Character literals
• String literals
• Boolean literals
– Each of them has a associated type that describes
how the values behave and how they are stored.
• Operators
– Is a symbol that takes one or more arguments and operates on them
to produce a result.
• Separators
– Separators are symbols used to indicate where groups of code are
divided and arranged.
– They basically define the shape and function of our code.

You might also like