Concept: Java System

Reading Time: 3 minutes


The smallest individual unit in a program is known as Token. Java comprises of five types of tokens:

  • Keywords
  • Identifiers
  • Literals
  • Separators
  • Operators

Keywords

Keywords are the words that convey a special meaning to the language compiler. These are reserved for special purpose and must not be used as normal identifiers names. Some important keywords in Java are:

Data Type Keywords
byteshortintlongchar
floatdoublebooleanvoid
Control Flow Keywords
ifelseswitchcasedefault
dowhileforbreakcontinue
return
Object Oriented Keywords
newclassinterfaceimplementsthis
Inheritance Keywords
superabstractsextends
Modifier Keywords
privatepublicprotectedfinalstatic

Identifiers

Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program viz. variables, objects, classes, functions, arrays, etc. Identifier forming rules of Java state the following:

  1. They are case sensitive.
  2. They can have unlimited letters, digits, underscore and dollar sign characters.
  3. They must not begin with a digit.
  4. They must not be a keyword.

Literals

Literals are data items that are fixed data values. Literals are also commonly called constants. Java allows six kinds of literals:

  • Integer literal
  • Floating point literal
  • Boolean literal
  • Character literal
  • String literal
  • The null literal

By default, integer literals are of type int. To specify a long literal, L or l is appended to the constant. For example, 12 is an int, but 12L is a long.

By default, floating-point literals are of type double. To specify a float literal, F or f is appended to the constant. For example, 10.19 is double, but 10.19F is float.

Seperators

The following nine characters are the separators used in Java: ( ) { } [ ] ; , .

Operators

The operations are symbols/words that trigger some operations. The objects of operations are referred to as operands. Java supports several types of operators:

  1. Arithmetic Operators
    • Unary Operators (+ −)
    • Binary Operators (+ − * / %)
  2. Increment/Decrement Operators (++ −−)
  3. Relational Operators (< > <= >= == !=)
  4. Logical Operators (&& || !)
  5. Assignment Operators (= += *= %=)
  6. Other Operators (?: [ ] . (type) new)

The following table lists some important operators with their description:

OperatorDescription
/Returns the quotient by dividing its first operand by the second.
%Returns the remainder by dividing its first operand by the second.
&&Returns true if both of its original expressions, i.e., its operands are true.
||Returns true if either of its original expressions, i.e., its operands are true.
=Assigns value of the right-side operand(s) to the left-side operand. Associativity is right to left.
==Returns true if the values of two operands are equal; else false. Associativity is left to right.
?:In an expression a? b:c, if operand ‘a’ is true operand ‘b’ is returned; otherwise ‘c’ is returned.
newIt is used to create a new instance of a class.
.It is used to access member of an object or class.

Operators also have two important characteristics:

  1. Operator Precedence: It determines the order in which expressions are evaluated. This, in some cases, can determine the overall value of the expression.
  2. Operator Associativity: It determines the grouping of operands and operators in an expression with more than one operator of the same precedence.

The following table lists some operators from highest to lowest precedence order:

HIGH TO LOW PRECENDENCE$\larr$ PRECENDENCE IS SAME ACROSS ROW $\rarr$
a++a--
++a--a+a-a!
a * aa / ba % b
a + ba - b
a < ba > ba <= ba >= b
a == ba != b
a && b
a || b
a ? b : c
a = ba += ba -= ba *= ba /= ba %= b