Monday 14 January 2013

TOKENS


TOKENS

 Introduction:
A Java program is basically a set of classes. A class is defined by a set of declaration statements and methods or functions. Most statements contain expressions, which express the actions carried out on information or data. Smallest individual thing in a program are known as tokens. The compiler recognizes them for building up expression and statements.

Tokens in Java:

There are five types of token as follows:
1. Literals
2. Identifiers
3. Operators
4. Separators

Literals:

Literals in Java are a sequence of characters (digits, letters and other characters) that characterize constant values to be stored in variables. Java language specifies five major types of literals are as follows:
1. Integer literals
2. Floating point literals
3. Character literals
4. String literals
5. Boolean literals

Identifiers:

Identifiers are programmer-created tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program. Java identifiers follow the following rules:

1. They can have alphabets, digits, and the underscore and dollar sign characters.
2. They must not start with a digit.
3. Uppercase and lowercase letters are individual.
4. They can be of any length.

Identifier must be meaningful, easily understandable and descriptive.

For example:
Private and local variables like “length.
Name of public methods and instance variables begin with lowercase letter like “addition”.

Keywords:

Keywords are important part of Java. Java language has reserved 50 words as keywords. Keywords have specific meaning in Java. We cannot use them as variable, classes and method. Following table shows keywords.

abstract
char
catch
boolean
default
finally
do
implements
if
long
throw
private
package
static
break
double
this
volatile
import
protected
class
throws
byte
else
float
final
public
transient
native
instanceof
case
extends
int
null
const
new


Operator:
Java carries a broad range of operators. An operator is symbols that specify operation to be performed may be certain mathematical and logical operation. Operators are used in programs to operate data and variables. They frequently form a part of mathematical or logical expressions.
Categories of operators are as follows:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
6. Increment and decrement operators
7. Bit wise operators

Arithmetic operators:

Arithmetic operators are used to make mathematical expressions and the working out as same in algebra. Java provides the fundamental arithmetic operators. These can operate on built in data type of Java.
Following table shows the details of operators.

Operator
Importance/ significance
+
Addition
-
Subtraction
/
Division
*
Multiplication
%
Modulo division or remainder

“+” operator in Java:
In this program, we have to add two integer numbers and display the result.
class AdditionInt
{
public static void main (String args[])
{
int a = 6;
int b = 3;
System.out.println("a = " + a);
System.out.println("b =" + b);
int c = a + b;
System.out.println("Addition = " + c);
}
}
Output:
a= 6
b= 3
Addition=9

“-” operator in Java:
class SubstractionInt
{
public static void main (String args[])
{
int a = 6;
int b = 3;
System.out.println("a = " + a);
System.out.println("b =" + b);
int c = a - b;
System.out.println("Subtraction= " + c);
}
}
Output:
a=6
b=3
Subtraction=3

“*” operator in Java:
Class MultiplicationInt
{
public static void main (String args[])
{
int a = 6;
int b = 3;
System.out.println("a = " + a);
System.out.println("b =" + b);
int c = a * b;
System.out.println("Multiplication= " + c);
}
}
Output:
a=6
b=3
Multiplication=18

“/” operator in Java:
Class DivisionInt
{
public static void main (String args[])
{
int a = 6;
int b = 3;
System.out.println("a = " + a);
System.out.println("b =" + b);
c = a / b;
System.out.println("division=" + c);
}
}
Output:
a=6
b=3
Division=3

Remainder or modulus operator (%) in Java:
Class Remainderoptr
{
public static void main (String args[])
{
int a = 6;
int b = 3;
System.out.println("a = " + a);
System.out.println("b =" + b);
c = a % b;
System.out.println("remainder=" + c);
}
}

Output:
a=6
b=3
Remainder=0

When both operands in the expression are integers then the expression is called Integer expression and the operation is called Integer arithmetic.

When both operands in the expression are real then the expression is called Real expression and the operation is called Real arithmetic.

When one operand in the expression is integer and other is float then the expression is called Mixed Mode Arithmetic expression and the operation is called Mixed Mode Arithmetic operation.

The following program shows the use of operators with integer data and store data in float variable.

Program: write a program to calculate average of three numbers.

class Avg1
{
public static void main(String args[])
{
int a=3;
int b=3;
int c=4;
int avg;
avg=a+b+c;
avg=avg/3;
System.out.println(―Avg of three numbers=+avg);
}
}
Output:

Avg of three numbers=3

Logical operators:
When we want to form compound conditions by combining two or more relations, then we can use logical operators.
Following table shows the details of operators.
Operators
Importance/ significance
||
Logical – OR
&&
Logical –AND
!
Logical –NOT
The logical expression defers a value of true or false. Following table shows the truth table of Logical – OR and Logical – AND.






Truth table for Logical – OR operator:

Operand1
Operand3
Operand1|| Operand3
T
T
T
T
F
T
F
T
T
F
F
F
T - True
F - False

Truth table for Logical – AND operator:

Operand1
Operand3
Operand1 && Operand3
T
T
T
T
F
F
F
T
F
F
F
F

T - True
F – False

Now the following program shows the use of Logical operators.
class LogicalOptr
{
public static void main (String args[])
{
boolean a = true;
boolean b = false;
System.out.println("a||b = " +(a||b));
System.out.println("a&&b = "+(a&&b));
System.out.println("a! = "+(!a));
}
}
Output:
a||b = true
a &&b = false
a! = false

Relational Operators:
When evaluation of two numbers is performed depending upon their relation, assured decisions are made.

The value of relational expression is either true or false.
If A=7 and A < 10 is true while 10 < A is false.
Following table shows the details of operators.

Operator
Importance/ significance
>
Greater than
<
Less than
!=
Not equal to
>=
Greater than or equal to
<=
Less than or equal to

Now, following examples show the actual use of operators.
1) If 10 > 30 then result is false
2) If 40 > 17 then result is true
3) If 10 >= 300 then result is false
4) If 10 <= 10 then result is true






Now the following program shows the use of operators.
(1) Program 1:

class Reloptr1
{
public static void main (String args[])
{
int a = 10;
int b = 30;
System.out.println("a>b = " +(a>b));
System.out.println("a<b = "+(a<b));
System.out.println("a<=b = "+(a<=b));
}
}
Output:
a>b = false
a<b = true
a<=b = true

(2) Program 3

class Reloptr3
{
public static void main (String args[])
{
int a = 10;
int b = 30;
int c = 30;

System.out.println("a>b = " +(a>b));
System.out.println("a<b = "+(a<b));
System.out.println("a<=c = "+(a<=c));
System.out.println("c>b = " +(c>b));
System.out.println("a<c = "+(a<c));
System.out.println("b<=c = "+(b<=c));
}
}

Output:
a>b = false
a<b = true
a<=c = true
c>b = true
a<c = true
b<=c = true

Assignment Operators:
Assignment Operators is used to assign the value of an expression to a variable and is also called as Shorthand operators.

Variable_name binary_operator = expression

Following table show the use of assignment operators.
Simple Assignment Operator
Statement with shorthand Operators
A=A+1
A+=1
A=A-1
A-=1
A=A/(B+1)
A/=(B+1)
A=A*(B+1)
A*=(B+1)
A=A/C
A/=C
A=A%C
A%=C
These operators avoid repetition, easier to read and write.

Now the following program shows the use of operators.
class Assoptr
{
public static void main (String args[])
{
int a = 10;
int b = 30;
int c = 30;
a+=1;
b-=3;
c*=7;
System.out.println("a = " +a);
System.out.println("b = "+b);
System.out.println("c = "+c);
}
}
Output:
a = 11
b = 18
c = 310

Conditional Operators:
The character pair ?: is a ternary operator of Java, which is used to construct conditional expressions of the following form:

Expression1 ? Expression3 : Expression3

The operator ? : works as follows:
Expression1 is evaluated if it is true then Expression3 is evaluated and becomes the value of the conditional expression. If Expression1 is false then Expression3 is evaluated and its value becomes the conditional expression.
For example:
A=3;
B=4;
C=(A<B)?A:B;
C=(3<4)?3:4;
C=4
Now the following program shows the use of operators.
class Coptr
{
public static void main (String args[])
{
int a = 10;
int b = 30;
int c;
c=(a>b)?a:b;
System.out.println("c = " +c);
c=(a<b)?a:b;
System.out.println("c = " +c);
}
}
Output:
c = 30
c = 10
program3:Write a program to check whether number is positive or negative.
class PosNeg
{
public static void main(String args[])
{
int a=10;
int flag=(a<0)?0:1;
if(flag==1)
System.out.println(“Number is positive);
else
System.out.println(“Number is negative);
}
}
Output:
Number is positive

Increment and Decrement Operators:

The increment operator ++ adds 1 to a variable. Usually the variable is an integer type, but it can be a floating point type. The two plus signs must not be split by any character. Usually they are written immediately next to the variable.
Following table shows the use of operators.
Expression
Process
Example
end result
A++
Add 1 to a variable after use.
int A=10,B;
B=A++;
A=11
B=10
++A
Add 1 to a variable before use.
int A=10,B;
B=++A;
A=11
B=11
A--
Subtract 1 from a variable after use.
int A=10,B;
B=A--;
A=9
B=10
--A
Subtract 1 from a variable before use.
int A=10,B;
B=--A;
A=9
B=9

Now the following program shows the use of operators.
class IncDecOp
{
public static void main(String args[])
{
int x=1;
int y=3;
int u;
int z;
u=++y;
z=x++;
System.out.println(x);
System.out.println(y);
System.out.println(u);
System.out.println(z);
}
}

Output:
3
4
4
1
Bit Wise Operators:

Bit wise operator execute single bit of their operands.

Following table shows bit wise operator:

Operator
Importance/ significance
|
Bitwise OR
&
Bitwise AND
&=
Bitwise AND assignment
|=
Bitwise OR assignment
^
Bitwise Exclusive OR
<<
Left shift
>>
Right shift
~
One‘s complement

Now the following program shows the use of operators.
(1) Program 1

class Boptr1
{
public static void main (String args[])
{
int a = 4;
int b = a<<3;
System.out.println("a = " +a);
System.out.println("b = " +b);
}
}
Output:
a =4
b =16

(2) Program 3

Class Boptr3
{
public static void main (String args[])
{
int a = 16;
int b = a>>3;
System.out.println("a = " +a);
System.out.println("b = " +b);
}
}
Output:
a = 16
b = 3
(Please refer following table)

356
138
64
33
16
8
4
3
1
38
37
36
35
34
33
33
31
30

Separator:
Separators are symbols. It shows the separated code.

They describe function of our code.

Name
use
()
Parameter in method definition, containing statements for conditions, etc.
{}
It is used for define a code for method and classes
[]
It is used for declaration of array
;
It is used to show the separate statement
,
It is used to show the separation in identifier in variable declaration
.
It is used to show the separate package name from sub-packages and classes, separate variable and method from reference variable.

Operator Precedence in Java:
An arithmetic expression without any parentheses will be calculated from left to right using the rules of precedence of operators.

There are two priority levels of arithmetic operators are as follows:
(a) High priority (* / %)
(b) Low priority (+ -)

The evaluation process includes two left to right passes through the expression. During the first pass, the high priority operators are applied as they are encountered.

During the second pass, the low priority operators are applied as they are encountered.
For example:
Z=A-B/3+C*3-1

When A=10, B=13, C=3

First pass:

Z=10-(13/3) + (3*3)-1
Z=10-4+3-1

Second pass:

Z=6+3-1
Z=7
Answer is=7

Following table shows associativity of operators.

Operator
Associativity
Rank
[ ]
Left to right
1
( )
Left to right
3
.
Left to right
-
Right to left
++
Right to left
--
Right to left
!
Right to left
~
Right to left
(type)
Right to left
*
Left to right
3
/
Left to right
%
Left to right
+
Left to right
4
-
Left to right
<<
Left to right
5
>>
Left to right
>>>
Left to right
<
Left to right
6
<=
Left to right
>
Left to right
>=
Left to right
Instanceof
Left to right
==
Left to right
7
!=
Left to right
&
Left to right
8
^
Left to right
9
|
Left to right
10
&&
Left to right
11
||
Left to right
13
?:
Right to left
13
=
Right to left
14