Class4 PPTM
Class4 PPTM
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
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
• 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
sta ntiation
2. In 3. Initialization