Tokens in Java

Tokens in Java


Tokens In Java Notes

Token – The smallest individual unit in a program is known as Token.

There are five tokens in Java. They are as follows:

Keywords

Keywords are the words that convey special meaning to the language compiler. These are reserved for special purpose and must not name be used as normal identifiers names. Example :- abstract, Boolean, byte, case, catch, try, char, class, const, continue, break, for, if, while etc. true, false and null are not keywords but reserved words.


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 programs viz. variables, objects, classes, functions, arrays.

Rules

  • Identifiers can have alphabets, digits, underscore (_) and dollar sign ($) characters.
  • They must not be a keyword or Boolean literal or null literal.
  • They must not begin with a digit.
  • They can be of any length.
  • Java is case sensitive i.e. lowercase and uppercase letters are treated differently.

Literals

Literals (often referred to as constants) are data items that are fixed data values. Java allows several kinds of literals:

  1. Integer literals – These are whole numbers without any fractional part. E.g. -7, 500, 9, -16 etc.
  2. Real literals – The numbers having fractional parts. E.g. -2.0, 55.974 etc.
  3. Boolean literals – The Boolean type has two values represented by the literals true and false formed from ASCII letters.
  4. Character literal – A character literal in Java must contain one character and must be enclosed within single quotation marks.
    Non-graphic characters are those characters that cannot be types directly from the keyboard. e.g. backspace, tabs, carriage return etc. These non-graphic characters can be represented by using escape sequences. An escape sequence is represented by one or more characters.

    Escape Sequence Non- graphic characters
    \n newline or linefeed
    \r carriage return
    \t horizontal tab
    \’ single quote
    \” double quote
  5. String literal – String literal is a sequence of zero or more characters surrounded by double quotes. Each character may be represented by escape sequence. E.g. abc”.
  6. Null literal – The null type has one value, the null reference, represented by literal, null.

Separators

The following nine ASCII characters are the separators(punctuators)

 (   )  {   }   [   ]   ;    ,   .

Operators

The following 37 tokens are the operators formed from ASCII characters

=     >       <       !     ~     ?     =     <=    >=    !=   
&&    ||      ++      --    +     -     *     /     &     |
^      %      <<      >>>   +=    -=    *=    /=    &=    %=
<<=   >>=     >>>=    ==    :     |=    ^=