Are You Feeling any difficulity while writing Core-Java Programs. Send those to me Here.

Tokens in Java

Tokens in Java

Unit Structure

3.1 Introduction
3.2 Tokens in Java
3.2.1 Identifiers
3.2.2 Litrals
3.2.3 Keywords
3.2.4 Operator
3.2.4.1 Arithmetic operators
3.2.4.2 Logical operators
3.2.4.3 Relational operators
3.2.4.4 Assignment operators
3.2.4.5 Conditional operators
3.2.4.6 Increment and decrement operators
3.2.4.7 Bit-wise operator
3.2.5 Separators
3.3 Operator Precedence in Java
3.4 Summary

3.1 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 indivisual thing in a program are known as tokens. The compiler recognizes them for building up expression and statements.

3.2 Tokens in Java:

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

3.2.1 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

3.2.2 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‖

3.2.3 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.


3.2.4 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

3.2.4.1 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

Now the following programs show the use of arithmetic operators.

“+” 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 opration is called Integer arithmetic. 
  • When both operands in the expression are real then the expression is called Real expression and the opration 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 opration is called Mixed Mode Arithmetic operation.

As we learn the Arithmetic operation on integer data and store data in integer variable. But 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

3.2.4.2 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.

The logical expression defer a value of true or false. Following table shows the truth table of Logical – OR and Logical – AND.

Truth table for Logical – OR operator:
T - True
F - False
Truth table for Logical – AND operator:
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

3.2.4.3 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.

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

3.2.4.4 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.

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

3.2.4.5 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

3.2.4.6 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

3.2.4.7 Bit Wise Operators:

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


3.2.5 Separator:

Separators are symbols. It shows the separated code.they describe function of our code.


3.3 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.


3.4 Summary:

In this unit, we learn the cocept of tokens in java.There are 4 types of tokens as we learn:
1. Literals
2. Identifiers
3. Operators
Types of operators are:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
6. Increment and decrement operators
7. Bit wise operator
We learn these operators with example.
4. separator



No comments:

Post a Comment