Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Unit III

Java as object oriented Programming Language

Prof. Sonali Patil khamkar


Topics
• Fundamentals of java, array
• String Handling
• Classes and methods
Fundamentals of java
• Features of java
 Simple
 Secure
 Platform Independent
 Object Oriented
 Distributed
 Portable
 High Performance
 Robust
 Multithreaded
 Interpreted
 Dynamic
Difference between C, C++,Java
Java Program Structure
Documentation Section

Package Statements Section

Import Statements Section

Class Definition

Main method class


{
Public Static void main(String[] args)
{
// main method definition
}
}
Variables
• A variable is an identifier that denotes the storage location.
• Syntax:
Data_type name_of variable[=initialization][,=initialization][,….];

• Example:
int a,b;
Char m=‘a’;
Byte k=12,p,t=22;
Keywords in java
abstract double Long Throw
assert Else Native Throws
Boolean Extends New transient
Byte Final Package Catch
Break For Super Char
Case Int Switch Class
default Interface this const

continue do float Private


try goto protected Void
if public volatile Implements
return while import Short
true instanceOf static false
null
Data types and size
Operators in java
Operator Type Category Precedence
postfix expr++ expr--
Unary ++expr --expr +expr -expr
prefix
~!
multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
Operators in java
Operator Type Category Precedence
logical AND &&
Logical
logical OR ||
Ternary ternary ?:
= += -= *= /= %= &= ^= |=
Assignment assignment
<<= >>= >>>=
Control Statements
• if Statement
• if else Statement
• while Statement
• do…while Statement
• switch case Statement
• for loop
If Statement
• Two types of if Statement
1.simple if Statement
2.Compound if Statement
1.Simple if Statement:
Syntax:
if(apply some condition)
statement
Example:
if(a>b)
system.out.println(“a is big”);
2. Compound if Statement:
syntax:
if(apply Some condition)
{
statement 1;
statement2;
statement3;
statement n;
}
If….else statement
• Syntax:
If(condition)
{
Statement
}
Else
{
Statement
}
While Statement
• Syntax:
while (condition)
{
Statement One;
}
Example of While Statement
public class whileTest
{
public static void main(String args[])
{
int i = 5;
while (i <= 15)
{
System.out.println(i);
i = i+2;
}
}
}
OutPut:5
7
9
11
13
15
Do..while
Syntax:
do{
//code to be executed
}while(condition);
Example
public class Main
{
public static void main(String args[])
{
int i = 20;
do
{
System.out.println(i);
i = i+1;
} while (i <= 20);
}
}
Switch Case Statement
Syntax:
Switch(variable/value/expression){
Case1 :
// statements [];
break;
Case2 :
// statements [];
break;…
default:
// statements [];
}
Example of Switch Statement
public class Music {
public static void main(String[] args)
{
int instrument = 4;
String musicInstrument;
// switch statement with int data type
switch (instrument) {
case 1:
musicInstrument = "Guitar";
break;
case 2:
musicInstrument = "Piano";
break;
case 3:
musicInstrument = "Drums";
break;
case 4:
musicInstrument = "Flute";
break;
default:
musicInstrument = "Invalid";
break;
}
System.out.println(musicInstrument);
}
}
For Loop
Syntax:
for (initialization; condition; increment/decrement)
{
statement;
}

Example:
public class forLoop
{
public static void main(String args[])
{
for (int i = 1; i <= 10; i++)
System.out.println(i);
}
}
Arrays
• Array is collection of similar Elements.
• Two types of array
1. One Dimensional array
2. Two Dimensional array
One Dimensional Array
• Syntax to Declare an Array in Java
dataType[] arr; (or)  
dataType []arr; (or)  
dataType arr[];  
• Instantiation of an Array in Java
arrayRefVar=new datatype[size]; 
Example of one Dimensional Array
class OnedimensionalStandard
{
public static void main(String args[])
{    
int[] a=new int[3];//declaration  
a[0]=10;//initialization  
a[1]=20;  
a[2]=30;  
//printing array  
System.out.println("One dimensional array elements are");    
System.out.println(a[0]);    
System.out.println(a[1]);    
System.out.println(a[2]);    
}
}
Two Dimensional Array
• Declaration – Syntax
data_type[][] array_name = new data_type[x][y];
• Initialization – Syntax: array_name[row_index]
[column_index] = value;
Example of Two Dimensional Array
class TwodimensionalStandard
{
public static void main(String args[])
{    
int[][] a={{10,20},{30,40}};//declaration and initialization  
System.out.println("Two dimensional array elements are");    
System.out.println(a[0][0]);    
System.out.println(a[0][1]);    
System.out.println(a[1][0]);    
System.out.println(a[1][1]);    
}
}

You might also like