Lecture 9 -
Lecture 9 -
Unit-I
Data Types
• Data Types, Declaring and Initialization Variables, Java Operators. Class, Object &
Methods, Constructors, Overloading constructors, This Keyword, Using Objects as
Parameters, Argument passing, returning objects,
2
Content
(Unit I)
Data Types
Chapter 1.2:
Lecture 1.2.4.-
1. Class
2. Objects
3
Objectives/Outcomes
4
Introduction to Class
• In the Java programming language, source files (.java files) are compiled into
(virtual)machine-readable class files which have a .class extension.
• Since Java is a platform-independent language, source code is compiled into an
output file known as byte code, which it stores in a .class file.
• If a source file has more than one class, each class is compiled into a separate .class
file.
• These .class files can be loaded by any Java Virtual Machine(JVM).
5
Simple Class
• Here is a class called Box that defines three member variables: width, height, and
depth.
class Box {
int width; int height; int depth;
}
• To create a Box object, you will use
a statement like the following:
Box myBox = new Box();
• myBox is an instance of Box.
• mybox contains its own copy of each instance variable, width, height, and depth, defined by the class.
• To access these variables, you will use the dot (.) operator.
6
Simple program using class
•Here is a complete program that uses the Box class:
class Box {
int width; int height; int depth;
}
public class SimpleclassMain {
public static void main(String args[ ]) {
Box myBox = new Box();
myBox.width = 10;
System.out.println("myBox.
width:"+myBox.width);
} 7
Declaring objects
•Object creation is a two-step process.
1. declare a variable of the class type.
2. acquire a physical copy of the object and assign it to that variable using the new operator.
•The new operator dynamically allocates memory for an object and returns a reference to it.
•The following lines are used to declare an object of type Box:
Box mybox
declares mybox as a reference to an object of type Box. mybox is null now.
mybox = new Box(); // allocate a Box object
allocates an actual object and assigns a reference to it to mybox.
•It can be rewritten to:
Box mybox = new Box();
• Any attempt to use a null mybox will result in a compile-time error.
8
Closer look @ new
•The new operator has this general form:
classVariable = new classname( );
The class name followed by parentheses specifies the constructor for the class.
A constructor defines what occurs when an object of a class is created.
Any changes made to the object through b2 will affect the object b1.
If b1 is been set to null, b2 will still point to the original object.
9
} }
Program(1)
The following program declares two Box objects:
class Box {
int width;
int height;
int depth;
}
public class InstanceMain {
public static void main(String args[]) {
Box myBox1 = new Box(); Box myBox2 = new Box();
myBox1.width = 10;
myBox2.width = 20;
System.out.println("myBox1.width:" +
myBox1.width);
System.out.println("myBox2.width:" +
myBox2.width);
} }
10
main statement
•public static void main(String args[ ])
• static - Java environment should be able to call this method without creating an
• instance of the class , so this method must be declared as static.
• main( ) - the name of the method, main because it's the main method.
• String args[ ] - arguments to this method. This method must be given an array of Strings, and the array will
be called 'args'.
11
Methods
•This is the general form of a method:
type methodName(parameter-list) {
// body of method
}
• type specifies the returned type.
• If the method does not return a value, its return type must be void.
• The parameter-list is a sequence of type and identifier pairs
separated by commas.
• Parameters receive the arguments passed to the method.
• If the method has no parameters, the parameter list will be empty.
12
Adding a method to class
class Box {
int width;
int height;
int depth;
void calculateVolume() { System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
public class MethodMain {
public static void main(String args[]) {
Box mybox1 = new Box(); mybox1.width
= 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox1.calculateVolume();
}
}
13
Constructors
A constructor initializes an object during
creation.
• It has the same name as the class.
• Constructors have no return type (not even
void). Class Rectangle public class ConstructorsMain {
double width; public static void main(String args[])
double height; { Rectangle mybox1 = new
Rectangle() { Rectangle(); double area;
width = 10; area = mybox1.area();
height = 10; System.out.println("Area is " + area);
} }
double area() { }
return width * height;
}
}
14
Constructors with Parameters
• The constructors can also have parameters.
• The parameters are used to set the initial states of the object.
class Rectangle { public class ConstParaMain {
{double
doublewidth;
width; public static void main(String args[]) {
double height; Rectangle mybox1 = new Rectangle(10, 20);
Rectangle(doubl
Rectangle(double double area;
ew,w,double
doubleh)h){ { width = w; area = mybox1.area();
height = System.out.println("Areais " + area);
h; }
double area() { } }
retur
n
widt
h*
heig
ht;
}
15
Constructors with object Parameters
class Rectangle { double width; double height; double area() {
Rectangle(Rectangle ob) { return width * height;
}
width = ob.width;
}
height = ob.height; public class ConsobjMain {
} public static void main(String args[])
Rectangle(double w, double h) { { Rectangle mybox1 = new Rectangle(10, 20);
Rectangle myclone = new Rectangle(mybox1);
width = w;
double area;
height = h; area = mybox1.area();
} System.out.println("Area of mybox1 is " +
Rectangle() { area);
width = -1; area = myclone.area();
height = -1; System.out.println("Area of clone is " + area);
} }
Rectangle(double len) { }
width = height = len;
}
16
} }
Overloading Constructors
class Rectangle { public class ConsOverloadingMain {
double width; public static void main(String args[])
double height; { Rectangle mybox1 = new Rectangle(10,
Rectangle(doubl 20); Rectangle mybox2 = new Rectangle();
e w, double h) { width = w; Rectangle mycube = new Rectangle(7);
height = h; double area = mybox1.area();
} System.out.println(area);
Rectangle() { area = mybox2.area();
width = -1; System.out.println(area);
height = -1; area = mycube.area();
} System.out.println(area);
Rectangle(double len) { } }
width = height = len;
}
double area() {
return width * height;
}
}
17
this keyword
this refers to the current object.
Rectangle(double w, double h) {
this.width = w;
this.height = h;
}
18
Program(2)
class Person{ else { nameTitle = name;
private String name; } return nameTitle;
@Override
private String title; public String toString() {
private String address; return "Info [" +
"name='" + name + '\'' +
public Person() {
} ']'; } }
public Person(String name) { public class ThisMain{
public static void main(String[] args) {
this.name = name; } Person person = new Person();
public String getName() { person.setName("Jack");
return name; } System.out.println(person);
public void setName(String name) { String nameTitle1 =
this.name = name; person.getNameWithTitle();
public String getNameWithTitle() { System.out.println("Name
Person person2 = newwith title: " +
Person("Sarah");
String nameTitle; nameTitle1);
String nameTitle2 = person2.getNameWithTitle();
if (title != null) { System.out.println("Name with title 2: " +
nameTitle = name + ", " + title; nameTitle2);
19
} } }
Returning a value
We can use return statement to return a value to the callers.
class Rectangle {
int width;
int height;
int
getAr
ea() {
r
e
t
u
r
n
w
i
d
t
h 20
Returning Object
class MyClass { public class RetobjMain {
int myMemberValue = 2; public static void main(String args[]) {
MyClass() { MyClass ob1 = new MyClass();
} ob1.myMemberValue =2; MyClass ob2;
MyClass doubleValue()
{ ob2 = ob1.doubleValue();
MyClass temp = new MyClass(); System.out.println("ob1.a: " +
temp.myMemberValue = temp.myMemberValue*2; ob1.myMemberValue);
return temp; System.out.println("ob2.a: " +
} ob2.myMemberValue);
}
ob2 = ob2.doubleValue();
System.out.println("ob2.a after second increase:
" + ob2.myMemberValue);
}
}
21
References
• https://www.geeksforgeeks.org/loops-in-java/#:~:text=ArrayList%20in%20Java-,L
oops%20in%20Java,some%20condition%20evaluates%20to%20true.&text=while
%20loop%3A%20A%20while%20loop,on%20a%20given%20Boolean%20conditi
on.
• https://www.javatpoint.com/java-do-while-loop
22
THANK YOU
23