Chapter 2 Part II Java Tokens
Chapter 2 Part II Java Tokens
Basics of Java
Java Tokens
1
Introduction
Most statements contain expressions, which describe the actions
carried out on data.
Smallest individual units in a program are known as tokens.
In simplest terms, a java program is a collection of tokens,
comments, and white spaces.
Java has five types of tokens: Reserved Keywords, Identifiers,
Literals, Separators and Operators .
2
1. Keywords
Are essential part of a language definition and can not be used as
names for variables, classes, methods and so on.
Java language has reserved 60 words as keywords.
3
2. Identifiers
Are programmer-designed tokens.
Are used for naming classes, methods, variables, objects,
labels, packages and interfaces in a program.
Java identifiers follow the following rules:
They can have alphabets, digits, and the underscore and
dollar sign characters.
They must not begin with a digit
Uppercase and lowercase letters are distinct.
They can be of any length.
4
POP QUIZ
1) $amount 5) score
2) 6tally 6) first Name
3) my*Name 7) total#
4) salary 8) this
5
3. Literals
Literals in Java are a sequence of characters(digits, letters and
other characters) that represent constant values to be stored in
variables.
Five major types of literals in Java:
I. Integer Literals: refers to a sequence of digits (decimal integer, octal
integer and hexadecimal integer)
V. Boolean Literals
6
4. Separators
Are symbols used to indicate where groups of code are divided
and arranged.
They basically define the shape and functions of our code.
Java separators include:
7
Contd.
III. Brackets [ ] :- are used to declare array types and for
dereferencing array values.
9
Java Operators
There are 8 different groups of operators in Java:
Arithmetic operators
Relational operators
Logical operators
Assignment operator
Increment/Decrement operators
Conditional operators
Bitwise operators
Special operators
10
A. Arithmetic Operators
Java has five basic arithmetic operators
Operator Meaning
+ Addition or unary plus
– Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
int x = 3;
int y = 5;
boolean result;
3) result = (x != x*y);
now result is assigned the value true because the product of
x and y (15) is not equal to x (3)
13
C. Logical Operators
Symbol Name
&& Logical AND
|| Logical OR
! Logical NOT
(x || y) evaluates to true
(true && x) evaluates to true
count = count - 1;
can be written as:
--count; or count--;
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ One’s complement
<< Shift left
>> Shift right
>>> Shift right with zero fill
20
7. Special Operators
Java supports some special operators of interest such as instanceof
operator and member selection operator(.).
int a=110;
float b=23.5;
(type-name)expression;
where type-name is one of the standard data types.
Example :
a=(int)21.3/(int)3.5; a will be 7
23
Operator Precedence and Associativity.
Operator Description Associativity Rank
. Member Selection
() Function call
Left to right 1
[] Array elements reference
- Unary Minus
++ Increment
-- Decrement
Right to left 2
! Logical negation
~ One’s complement
(type) Casting
* Multiplication
Left to right 3
/ Division
% Modulus
24
Contd.
Operator Description Associativity Rank
+ Addition
Left to right 4
- Subtraction
<< Left Shift
Left to right 5
>> Right Shift
>>> Right shift with zero fill
< Less than
<= Less than or equal to
> Greater than Left to right 6
>= Greater than or equal to
instanceof Type comparison
== Equality
Left to right 7
!= Inequality
25
Operator Precedence and Associativity.
Operator Description Associativity Rank
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
26
//Demonstration of Java Expressions
public class DemoExpress
{
public static void main(String[] args)
{
System.out.println("===== BEGINNING OF THE PROGRAM =====\
n");
//Declaration and Initialization
int a=10,b=5,c=8,d=2;
float x=6.4f,y=3.0f;
//Order of Evaluation
int answer1=a*b+c/++d;
int answer2=--a*(b+++c)/d++;
//Type Conversion
float answer3=a/c;
float answer4=(float)a/c;
float answer5=a/y;
27
//Modulo Operations
int answer6=a%c;
float answer7=x%y;
//Logical Operations
boolean bool1=a>b && c>d;
boolean bool2=a<b && c>d;
boolean bool3=a<b || c>d;
boolean bool4=!(a-b==c);
System.out.println("Order of Evaluation");
System.out.println("a*b+c/++d + "+answer1);
System.out.println("--a*(b+++c)/d++ = " +answer2);
System.out.println("================");
System.out.println("Type Conversion");
System.out.println(" a/c = "+answer3);
System.out.println("(float)a/c = " + answer4);
System.out.println(" a/y = " + answer5);
28
System.out.println("================");
System.out.println("Modulo Operations");
System.out.println(" a%c = "+answer6);
System.out.println(" x%y = "+answer7);
System.out.println("================");
System.out.println("Logical Operations");
System.out.println(" a>b && c>d = "+bool1);
System.out.println(" a<b && c>d = "+bool2);
System.out.println(" a<b || c>d = "+bool3);
System.out.println(" !(a-b==c) = "+bool4);
System.out.println("================");
System.out.println("Bitwise Operations");
29
//Shift Operators
int l=8, m=-8,n=2;
System.out.println(" n & 2= "+(n&2));
System.out.println(" l | n= "+(l|n));
System.out.println(" m | n= "+(m|n));
System.out.println(" l >> 2= "+(l>>2));
System.out.println(" l >>> 1= "+(l>>>1));
System.out.println(" l << 1= "+(l<<1));
System.out.println(" m >> 2= "+(m>>2));
System.out.println(" m >>> 1= "+(m>>>1));
System.out.println("\n===== END OF THE PROGRAM =====");
30
POP QUIZ
1) What is the value of number?
int number = 5 * 3 – 3 / 6 – 9 * 3; -12
32
Variables
A variable is an identifier that denotes a storage location used to
store a data value.
Unlike constants, that remain unchanged during the execution
of a program, a variable may take different values at different
times during the execution of the program.
It is good practice to select variable names that give a good
indication of the sort of data they hold:
For example, if you want to record the size of a hat,
hatSize is a good choice for a name whereas qqq would
be a bad choice.
33
Contd.
Variable names may consist of alphabets, digits, the
underscore (_) and dollar ($) characters, subject to the
following conditions:
1. They should not begin with a digit.
2. Keywords should not be used as a variable name.
3. White spaces are not allowed.
4. Uppercase and lowercase are distinct. i.e. A rose is
not a Rose is not a ROSE.
34
5. Variable names can be of any length.
Data Types
Every variable in Java has a data type.
Data types specify the size and type of values that can be stored.
Java language is rich in the data types.
Java data types are of two type:
Primitive Data Types (also called intrinsic or built-in data
types)
Non-Primitive data Types (also known as Derived or
reference types)
35
Data Types in Java
Primitive Non-Primitive
(Intrinsic) (Derived)
Interfaces
Integer Floating-Point Character Boolean
37
A. Integer Data types
There are four data types that can be used to store integers.
The one you choose to use depends on the size of the number that
we want to store.
38
B. Floating-Point Types
Integer types can hold only whole numbers and therefore we need
another type known as floating point type to hold numbers
containing fractional parts.
There are two data types that can be used to store decimal values
(real numbers).
39
C. Character Type
Is used to store character constants in memory.
Java provides a character data type called char
The char data type assumes a size of 2 bytes but, basically, it
can hold only a single character.
3. The place of declaration (in the program) declares the scope of the variable.
Example:
int count, x,y; //Declaration
char firstLetterOfName = 'e' ; // Declaration & initialization
42
Assigning Values to Variables
A variable must be given a value after it has been declared
this done by using an assignment statement.
Assignment Statement
A simple method of giving value to a variable is through the
assignment statement as follows:
variableName = value;
Example: x = 123, y = -34;
It is possible to assign a value to a variable at the time of
declaration as: type variableName = value;
43