0% found this document useful (0 votes)
22 views26 pages

Class4 PPTM

The document provides an overview of Java tokens, including identifiers, literals, operators, and separators. It explains the structure and rules for identifiers, various types of operators, and control flow statements such as if statements and loops. Additionally, it covers data types, implementing a Java program, command line arguments, variable types, class definitions, and object creation in Java.

Uploaded by

nsrgperumal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views26 pages

Class4 PPTM

The document provides an overview of Java tokens, including identifiers, literals, operators, and separators. It explains the structure and rules for identifiers, various types of operators, and control flow statements such as if statements and loops. Additionally, it covers data types, implementing a Java program, command line arguments, variable types, class definitions, and object creation in Java.

Uploaded by

nsrgperumal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Tokens in JAVA

Tokens
• Reserved keywords
• Identifiers
• Literals
• Operators
• Seperators
Identifiers
Can be a class name, variable name, method name, package
name, constant name, and more.
A valid identifier must have characters [A-Z] or [a-z] or numbers
[0-9], and underscore(_) or a dollar sign

There should not be any space in an identifier.

An identifier should not contain a number at the starting.

An identifier should be of length 4-15 letters only. However, there is no limit


on its length. But, it is good to follow the standard conventions.

We can't use the Java reserved keywords as an identifier


Literals
In Java, literals are the constant values that appear directly in the
program.
It can be assigned directly to a variable.

Java has various types of literals.


Operators in Java
Operator in Java is a symbol that is used to perform
operations. For example: +, -, *, / etc.

There are many types of operators in Java which are


given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>=
>>>=
Seperators
Encloses arguments in method definitions and
calling; adjusts precedence in arithmetic
() expressions; surrounds cast types and delimits
test expressions in flow control statements
defines blocks of code and automatically initializes
{} arrays
[] declares array types and dereferences array values
; terminates statements
separates successive identifiers in variable
, declarations; chains statements in the test, expression
of a for loop
Selects a field or method from an object; separates
. package names from sub-package and class names
: Used after loop labels
If Statement:
Simple if statement
if-else statement
if-else-if ladder
Nested if-statement

Simple if statement
if(condition)
{
statement 1; //
executes when condition is true
}
if-else statement

if(condition)
{
statement 1; //executes when condition is true
}
else
{
statement 2; //
executes when condition is false
}
if-else-if ladder

if(condition 1)
{
statement 1; //executes when condition 1 is true
}
else if(condition 2)
{
statement 2; //executes when condition 2 is true
}
else
{
statement 2; //
executes when all the conditions are false
}
Nested if-statement

if(condition 1)
{
statement 1; //executes when condition 1 is true
if(condition 2)
{
statement 2; //
executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Switch Statement:

switch (expression)
{
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;

default:
default statement;
}
Loop Statements

1.for loop
2.while loop
3.do-while loop

for loop
for(initialization; condition ;
increment/decrement)
{
//block of statements
}
for(data_type var : array_name/collection_name)
{
//statements
}
while loop

while(condition)
{
//looping statements
}

do-while loop

do
{
//statements
} while (condition);
Data Type Default Default size
Value
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Implementing a JAVA program
• Creating 🡪 clasname.java
• Compiling 🡪 javac classname.java
🡪classname.class
• Running 🡪 java classname
• Command Line arguments
• Variable types
• Types of class definition
• Creating an object
– Declaration
– Instantiation
– Initialization
• Constructor
– Types
Command Line arguments

class CmdLineArgDemo
{
public static void main(String a[])
{
System.out.println("Number of arguments : "+a.length);
for(String i:a)
{
System.out.println("java is "+i);
E:\oop pgm>java CmdLineArgDemo simple portable
} secured
} Number of arguments : 3
java is simple
} java is portable
java is secured
Variable types
class VariableDemo
{
int instanceVar;
static int classVar;
void display()
{
int localVar=100;
System.out.println("Am a local variable with value"+localVar);

}
public static void main(String a[])
{
VariableDemo vd=new VariableDemo();
vd.instanceVar=5;
VariableDemo.classVar=200;
vd.display();
System.out.println("Am a instace variable with value"+vd.instanceVar);
System.out.println("Am a class variable with value"+VariableDemo.classVar);
}
E:\oop pgm>javac VariableDemo.java

E:\oop pgm>java VariableDemo


Am a local variable with value 100
Am a instace variable with value 5
Am a class variable with value 200
Types of class definition

• Simple class
class A { }
• Class is a sub class of another class
class B extends A { }
• Class implements a specific interface
class C extends B implements Interface1 { }
• Java implements any number of interface but
extends only one class
• Always extends has to preceed implements
Creating an object

• 3 steps
– Declaration - declaration with a variable name with an
object type.
– Instantiation - allocate memory space for a new object.
The 'new' keyword is used to create the object
– Initialization - The 'new' keyword is followed by a call to
a constructor. This call initializes the new object.
1. Declaration

VariableDemo vd=new VariableDemo();

sta ntiation
2. In 3. Initialization

You might also like