Java – Tokens

Java – Tokens 2017-08-08T23:58:34+00:00

Java Tokens :

A token is the smallest element of a program that is meaningful to the compiler. (Actually, this definition is true for all compilers, not just the Java compiler.) These tokens define the structure of the Java language. When you submit a Java program to the Java compiler, the compiler parses the text and extracts individual tokens.

Java tokens can be broken into five categories: identifiers, keywords, literals, operators, and separators. The Java compiler also recognizes and subsequently removes comments and whitespaces.

Identifiers :

“Identifiers means a sequence of uppercase(A,B,C,……,Y,Z) and lowercase(a,b,c,…..,y,z) letters, numbers(0,1 ,2,……,9), or the underscore(_) and dollar-sign($) characters and must not begin with a number.” Identifiers are tokens that represent names. These names can be assigned to variables, methods, and classes to uniquely identify them to the compiler.

Valid and invalid Java identifiers.

Valid

Invalid

HelloWorld

Hello World (uses a space)

Hi_JAVA

Hi JAVA! (uses a space and punctuation mark)

value3

3value(begins with a number)

Tall

short (this is a Java keyword)

$age

#age (does not begin with any other symbol except _ $ )

NOTE : For Java identifiers, you should follow a few stylistic rules to make Java programming easier and more consistent. It is standard Java practice to name multiple-word identifiers in lowercase except for the beginning letter of words in the middle of the name. For example, the variable firstValue is in correct Java style; the variables firstvalue, FirstValue, and FIRSTVALUE are all in violation of this style rule.

Another more critical naming issue regards the use of underscore and dollar-sign characters at the beginning of identifier names. Using either of these characters at the beginning of identifier names is a little risky because many C libraries use the same naming convention for libraries, which can be imported into your Java code. A good use of the underscore character is to use it to separate words where you normally would use a space (Hi_JAVA).

Keywords :

“It is a special type of reserved word for a specific purpose which can not be use as a identifier means cannot be used as names for a variable, class, or method.”

There are 49 reserved keywords currently defined in the Java language (see the following table).

abstract

double

int

switch

assert

else

interface

synchronized

boolean

extends

long

this

break

false

native

throw

byte

final

new

transient

case

finally

package

true

catch

float

private

try

char

for

protected

void

class

goto

public

volatile

const

if

return

while

continue

implements

short

default

import

static

do

instanceof

super

The keywords const and goto are reserved but not used. In the early days of Java,several other keywords were reserved for possible future use.

In addition to the keywords, Java reserves the following: true, false, and null.

These are values defined by Java. You may not use these words for the names of variables, classes, and so on.

Separators :

Separators are used to inform the Java compiler of how things are grouped in the code. For example, items in a list are separated by commas much like lists of items in a sentence. The most commonly used separator in Java is the semicolon. As you have seen, it is used to terminate statements.

 

Symbol

Name

Purpose

;

Semicolon

Terminates statements.

,

Comma

Separates consecutive identifiers in a variable

declaration.

Also used to chain statements together inside a for statement.

{ }

Braces

Used to contain the values of automatically initialized arrays.

Also used to define a block of code, for classes, methods, and local scopes.

( )

Parentheses

Used to contain lists of parameters in method definition and invocation.

Also used for defining precedence in expressions, containing expressions in control statements.

Also used for surrounding cast types.

[ ]

Brackets

Used to declare array types.

Also used when dereferencing array values.

.

Period

Used to separate package names from subpackages and classes Also used to separate a variable or method from a reference variable.

 

Literals :

We describe this topic in next chapter named Literals.

Comments and Whitespaces :

The comments and whitespaces are removed by the Java compiler during the tokenization of the source code. White space consists of spaces, tabs, and linefeeds. All occurrences of spaces, tabs, or linefeeds are removed by the Java compiler, as are comments. Comments can be defined in three different ways, as shown in Table.

Types of comments supported by Java.

Type

Syntax

Usage

Example

Single-line

// comment

All characters after the // up to the end of the line are ignored.

//This is a Single-line style comment.

Multiline

/* comment */

All characters between /* and */ are ignored.

/* This is a Multiline style comment.

Documentation

/** comment */

Same as /* */, except that the comment can be used with the javadoc tool to create automatic documentation.

/** This is a javadoc style comment. */

Operators :

Operators means specify an evaluation to be performed on a data (Operands). The operators supported by Java is as under :

+

*

/

%

&

|

^

~

&&

||

!

<

>

<=

>=

<<

>>

>>>

=

?

++

==

+=

-=

*=

/=

%=

&=

|=

^=

!=

<<=

>>=

>>>=

.

[

]

(

)

Just seeing these operators probably doesn’t help you a lot in determining how to use them. Don’t worry-you’ll learn a lot more about operators and how they are used in the next chapter  “Operators”.

Prev Next