Professional Documents
Culture Documents
SLOT NO : S1 & S9
UNIT 1 : BASICS OF OOP and JAVA
Faculty Name : Dr. Murali Dhar M S
Academic Year : 2022-2023
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Syllabus
B. Pre-requisite courses:
10210CS101 - Problem Solving using C
Software
Evaluation
Ernest Tello, A well known writer in the field of artificial intelligence, compared
the evolution of software technology to the growth of the tree. Like a tree, the
software evolution has had distinct phases “layers” of growth.
JDK 1.1(1997)
JDK 1.2(1998)
JDK 1.3(2000)
JDK 1.4(2002)
JDK 1.5(2004)
JDK 1.6(2006)
JDK 1.7(2011)
JDK 1.8(2014)
JDK 1.9 (2017)
JDK 1.10 (March 2018)
JDK 1.11(Sep 2018)
JDK 1.12(March 2019)
JDK 1.13 (September 2019)
JDK 1.14(March 2020)
JDK 1.15 (September 2020)
Java is also an
object-oriented language.
C++ is an object-oriented However, everything (except
language. However, in the C fundamental types) is an
Object-oriented
language, a single root object in Java. It is a single
hierarchy is not possible. root hierarchy as everything
gets derived from
java.lang.Object.
Abstract machine.
provides runtime environment in which java bytecode can be
executed.
Platform dependent
The JVM performs following operation:
Loads code
Verifies code
Executes code
Provides runtime environment
JVM provides definitions for
Memory area
Class file format
Register set
Garbage-collected heap
Fatal error reporting etc.
To Set Path
set path=C:\Program Files\Java\jdk1.8.0_121\bin
export JAVA_HOME=/Library/Java/Home
echo $JAVA_HOME
To compile
javac FirstJavaProgram.java
To Run
java FirstJavaProgram
Program
mer
Executable
Object Code
Source Code
Code
Text Compil linker
Editor er
.c .o file a.out
file file
Notepa gc
d, c
emacs,v
i
Department of Computer Science and Engineering
Java is Compiled and Interpreted
Program Hardware
mer and
Operating
System
Source Code Byte
Text Compil Code Interp
Editor er reter
.java .class
file file
Notepad, javac java
emacs,vi appletviewer
netscape
class UnaryOperator
{
public static void main(String[] args)
{
double number = 5.2, resultNumber;
Output?
boolean flag = false;
System.out.println("+number = " +
+number);
System.out.println("-number = " + -
number);
System.out.println("number = " + +
+number);
System.out.println("number = " + --
number);
System.out.println("!flag = " + !flag);
}
}
Department of Computer Science and Engineering
Logical Operator Example
class LogicalOperator
{
public static void main(String[] args)
{
int number1 = 1, number2 = 2, number3 Output?
= 9;
boolean result;
result = (number1 > number2) ||
(number3 > number1);
System.out.println(result);
result = (number1 > number2) &&
(number3 > number1);
System.out.println(result);
}
}
class ConditionalOperator
{
public static void main(String[] args)
{
int februaryDays = 29;
String result;
result = (februaryDays == 28) ? "Not a leap year" :
"Leap year";
System.out.println(result);
}
Output?
}
}
Department of Computer Science and Engineering
}
Nested IF-Else
Example
class NestedIfDemo Nested if statements:
{ if(condition){
public static void main(String args[]) //code to be executed
{ if(condition){
int i = 10; //code to be executed
}
if (i == 10) else
{ {
// First if statement //false code
if (i < 15) }
System.out.println("i is smaller than }
15"); else
{
// Nested - if statement //false code
// Will only be executed if statement }
above
// it is true Output?
if (i < 12)
System.out.println("i is smaller than
12 too");
else
System.out.println("i is greater than
15");
} Department of Computer Science and Engineering
Continue…….
SYNTAX
public class Sample {
for(initialization;condition;incr/ public static void main(String[] args) {
decr)
{
// for(int i=0;i<=100;i++){
statement or code to be execu System.out.println(i);
ted }
} }
}
Output?
class Student{
Syntax for class: int id;
class <class_name>{ String name;
fields or attributes; void display()
methods; {
System.out.println("id is:"+id+"name
} is:"+name);
Syntax for creating object: }
class_name obj_name=new }
class_name(params); class TestStudent2{
Obj_name.instance_variable; public static void main(String args[]){
Student s1=new Student();
Obj_name.method1() s1.id=101;
s1.name=“Raja";
System.out.println(s1.id+" "+s1.name);
s1.display();
}
}
Output?
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
class A{
private int data=40;
public void msg(){System.out.println("Hello java");}
}
class Simple{
public static void main(String args[]){ Output?
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//No error
}
}
synchronized
Implicitly by JVM
Explicitly by programmer
Parameterized constructor
A constructor which has a specific
number of parameters is called a
parameterized constructor.
Syntax:
Access_specifier Class_name()
{
// intitialization code
}
Syntax:
Access_specifier Parameterized Constructor Example:
Class_name(parameters list) class Student4{
{ int id;
String name;
// intitialization code
Student4(int i,String n)
} {
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Kara
n");
s1.display();
}
}
class Adder{
int add(int a, int b)
{
return a+b;
}
double add(double a, double b)
{
return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
Adder a=new Adder();
System.out.println(a.add(11,11));
System.out.println(a.add(12.3,12.6));
}}
Department of Computer Science and Engineering
Type Casting
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee; Output?
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Static keyword
The static keyword in Java is used for memory management mainly.
We can apply static keyword with variables, methods, blocks and nested
classes.
The static keyword belongs to the class rather than an instance of the
class.
The static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class
Counter2(){
count++;
a=10;
System.out.println(count);
}
class A2{
static Output?
{
System.out.println("static block is invok
ed");
}
public static void main(String args[])
{
System.out.println("Hello main");
}
}
arrayRefVar=new datatype[size];
Initialization:
arrayRefVar[0]=10
Department of Computer Science and Engineering
1D Example
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20; Output?
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//
length is the property of array
System.out.println(a[i]);
} Department of Computer Science and Engineering
Multi Dimensional
Array
Array having more than one index
data is stored in row and column based index (also known as matrix form).
Example to instantiate Multidimensional Array in
Java
Syntax to Declare Multidimensional
Array in Java int[][] arr=new int[3][3];//3 row and 3 column
dataType[][] arrayRefVar; (or) Example to initialize Multidimensional Array in
dataType [][]arrayRefVar; (or) Java
dataType arrayRefVar[][]; (or)
Syntax for Instantiation arr[0][0]=1;
arrayRefVar=new dataType[size][size]; arr[0][1]=2;
Both in same statement: arr[0][2]=3;
dataType[][] arrayRefVar arr[1][0]=4;
arrayRefVar=new dataType[size][size]; arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Declarion+instantiation+intialization
Int[][] arr={{1,2,3},{4,5,6},{7,8,9}}
Department of Computer Science and Engineering
2D Array Representation in
memory
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++) Output?
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}