Java | Tokens

05 min read

Tokens:

The smallest individual units in a program are known as tokens. Java program is a collection of tokens, comments, and whitespaces. There are 5 types of tokens

  • Reserved Keywords
  • Identifiers
  • Literals
  • Operators
  • Separators

1.Whitespaces: 

Java is a free-form language ie., no need to follow any special indentation rules. (e0) any Java program can also be written in a single line if there was at least one whitespace character between each ()Calera. White space is a space tab or a newline.

2. Comments:

  • // - a single line comment.
  • /*...... */ - a block comment
  • /** .....*/ - Documentation comment

It is used to generate the documentation automatically. The 'javadoc' tool is used for this. This tool or utility parses the entire program and finds the /** and */ symbols and take the comments inside the block for documentation. It creates an HTML, file with these comments and prepares the documentation for the program. This documentation generated can be viewed using a browser such as Netscape Navigator or Internet Explorer.

A documentation comment can also include HTML, tags, as well as special tags beginning with @ that are used to document methods and classes in a standard form. The @ is followed by a keyword that defines the purpose of the tag.

Example:

@author — to define the author of the code

@deprecated — to denote that they should not be used in new applications

@exception — to document exceptions that the code can throw

@param — to describe the parameters for a method

@throws — a synonym for @exception

@version — to describe the current version of the code

@return — to document the value returned from a method

Apart from these specified tags, all HTML tags except the header tags can also be used

3. Reserved Keywords:

There are around 50 keywords and they cannot be used as identifiers. 'const' and 'goto' are reserved but they are not used. 

abstract

continue

goto

package

synchronized

assert

default

if

private

this

Boolean

do

implements

protected

throw

break

double

import

public

throws

byte

else

instan.Ceof

return

transient

case

extends

int

short

try

catch

final

interface

static

void

char

finally

long

strictfp

volatile

class

float

native

super

while

const

for

new

switch

 

 In addition to these keywords, Java reserves the following:

true, false, null - Though they appear to be keywords, 'true and false' are Boolean literals & 'null' literal respectively; the values defined by Java.

4. Identifiers:

  • Names given to classes, variables, methods, arrays, etc.
  • Case sensitive.
  • Uppercase and lowercase letters, numbers and underscore ('_') are used.
  • $ is also used (not intended for general use).
  • The names must begin with an alphabet or an underscore character or a dollar sign. Cannot start with a digit.
  • A keyword cannot be used as an identifier name.
  • White space and special characters are not allowed.

5. Literals or Constants:

These are fixed values directly used in the program and they remain unchanged during the execution of the program.

  • Logical literal — (eg) true, false
  • Numeric or arithmetic literal — (eg) 100, 98.6
  • String literal — "test"
  • Null literal - null

6. Separators:

Used for grouping and separating.

  • ; - statement terminator
  • , - separates consecutive identifiers
  • . - to separate the class name & method name; package name & class name
  • () - to group, the arguments supplied to a method
  • { } - to list values of automatically initialized arrays, to separate a block of code                     
  • [ ] - to give the array index and to specify the size of an array

7. Operators:

  • Arithmetic Operator
  • Bitwise Operator
  • Relational Operator
  • Boolean logical operator
  • Short circuit logical operator 
  • Ternary operator
  • instanceof operator
  • Dot operator
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error