0% found this document useful (0 votes)
11 views

Lecture-5 Passing Objects

Uploaded by

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

Lecture-5 Passing Objects

Uploaded by

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

Lecture – 5

Passing Objects
Primitive Parameters
• Primitive types: boolean, byte, char, short, int, long, float, double
• In Java, all primitives are passed by value. This means a copy of
the value is passed into the method
• Modifying the primitive parameter in the method does NOT
change its value outside the method
Object Parameters
• Objects can be passed natively, just like primitives
• It is often misstated that Object parameters are passed by
Reference.
• While it is true that the parameter is a reference to an Object, the
reference itself is passed by Value.
• What we pass in method is a handle of an object, and in the called
method a new handle created and pointed to the same object.
• Now when more than one handles tied to the same object, it is
known as aliasing.
5-3
Object Parameters

toy2.changer(anObject);

5-4
class ToyClass{
private String name;
private int number;
public ToyClass(String initialName, int initialNumber){
name = initialName;
number = initialNumber;
}
public ToyClass(){
name = "No name yet.";
number = 0;
}
public static void changer(ToyClass aParameter){
aParameter.name = "Hot Shot";
aParameter.number = 42;
}
public void tryToMakeEqual(int aNumber){
aNumber = number;
}
public boolean equals(ToyClass otherObject){
return ((name.equals(otherObject.name)) && (number == otherObject.number) );
}
public String toString(){
return (name + " " + number);
}
}
5-5
Object Parameters - Memory Picture(Part 1 of 3)

5-6
Memory Picture for Display 5.14
(Part 2 of 3)

5-7
Memory Picture for Display 5.14
(Part 3 of 3)

5-8
public class Complex{ Return Objects
private double real;
private double img;
//Default Constructor
public Complex(){
From Methods
real = 0.0;
img = 0.0;
}
//Overloaded Constructor
Main class
public Complex(double r, double im){
real = r; Complex c1 = new Complex(11 , 2.3);
img = im; Complex c2 = new Complex(9 , 2.7);
} System.out.println("Complex-1: "+c1);
//Adding Two Complex objects and return Complex object System.out.println("Complex-2: "+c2);
public Complex addComplex(Complex b){
double r = real + b.real; Complex c3 = c1.addComplex(c2);
double i = img + b.img;
//Create a temporary Complex to return it System.out.println("Complex-3: "+c3);
Complex temp = new Complex(r , i);
return temp;
//Or return new Complex(r , i);
}
//toString Method to display object values in instance variables Complex-1: 11.0 2.3
public String toString(){
Complex-2: 9.0 2.7
return(real+" "+img); Complex-3: 20.0 5.0
}
}
5-9
public class Point{ Objects can be passed natively, just like primitives
public int x;
public int y;
public Point(int a, int b){
x = a; The method “tricky” is not performing
y = b;
} swapping of object passed by main(),
public Point(){}
public void tricky(Point pa , Point pb){ it swaps the objects in the function
Point temp = new Point();
temp = pa; “tricky”
pa = pb;
pb = temp;
System.out.println("pa.X: "+pa.x + " pa.Y: "+pa.y);
System.out.println("pb.X: "+pb.x + " pb.Y: "+pb.y);
}

Main Method
Point pnt1 = new Point(1,2); pnt1.X: 1 pnt2.Y: 2
Point pnt2 = new Point(3,4); pnt2.X: 3 pnt2.Y: 4
System.out.println("pnt1.X: "+pnt1.x + " pnt2.Y: "+pnt1.y);
System.out.println("pnt2.X: "+pnt2.x + " pnt2.Y: "+pnt2.y);
pa.X: 3 pa.Y: 4
pb.X: 1 pb.Y: 2
pnt1.tricky(pnt1 , pnt2); pnt1.X: 1 pnt1.Y: 2
System.out.println("pnt1.X: "+pnt1.x + " pnt1.Y: "+pnt1.y); pnt2.X: 3 pnt2.Y: 4
System.out.println("pnt2.X: "+pnt2.x + " pnt2.Y: "+pnt2.y);
The equals Method
• When the == operator is used with reference variables, the memory
address of the objects are compared.
• The contents of the objects are not compared.
Stock stock1 = new Stock("GMX", 55.3);
Stock stock2 = new Stock("GMX", 55.3);
if (stock1 == stock2) // This is a mistake.
System.out.println("The objects are the same.");
else
System.out.println("The objects are not the same.");

• In above code segment only the addresses of the objects are compared.
9-12
The equals Method
• Java expects certain methods, such as equals to be in all, or almost all, classes
• The purpose of equals, a boolean valued method, is to compare two objects of
the class to see if they satisfy the notion of "being equal"
– Note: You cannot use == to compare objects
public boolean equals(ClassName objectName)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 4-13


The equals Method
• Instead of using the == operator to compare two Stock
objects, we should use the equals method.
public boolean equals(Stock object2)
{
boolean status;

if(symbol.equals(Object2.symbol) && sharePrice == Object2.sharePrice)


status = true;
else
status = false;
return status;
}

• Now, objects can be compared by their contents rather than by


their memory addresses.
9-14
Methods That Copy Objects
• There are two ways to copy an object.
– You cannot use the assignment operator to copy reference types

– Reference only copy (shallow Copy)


• This is simply copying the address of an object into another reference variable.
Stock stock1 = new Stock("GMX", 55.3);
Stock stock2 = stock1;

Deep copy (correct)


• This involves creating a new instance of the class and copying the values from one object into
the new object.

9-15
Copy Constructors
• A copy constructor accepts an existing object of the same class and clones it
public Stock(Stock object1)
{
if (object1 == null) //Not a real stock.
{
System.out.println("Fatal Error.");
System.exit(0);
}
symbol = object1.symbol;
sharePrice = object1.sharePrice;
}

// Create a Stock object


Stock company1 = new Stock("XYZ", 9.62);

//Create company2, a copy of company1


Stock company2 = new Stock(company1);

9-16
this Pointer

• this is a reference variable that refers to the current object


• this can be used to refer current class instance variable
• this can be used to invoke current class method
• this() can be used to invoke current class constructor
• this can be passed as an argument in the method call
• this can be passed as argument in the constructor call
• this can be used to return the current class instance from the method

5-17
//this() can be used to invoke current class constructor
// Call to this() must be the first statement in constructor

public class Student{


private int rollNo;
private String name;
private String course;
private double fee;

public Student(){
rollNo = 0;
name = null;
course = null;
fee =0.0;
}
public Student(int rollNo, String name, String course){
this.rollNo = rollNo;
this.name = name;
this.course = course;
}
public Student(int rollNo, String name, String course, double fee){
this(rollNo, name, course);
this.fee = fee;
}
public void display(){
System.out.println(this.rollNo+" "+ this.name+" "+this.course+" "+ this.fee);
}
}
5-18
Important Points
• If another object is required for the operation of a method , we
need to pass it through the argument.
• Class name is a user defined type.
• Class references can be used as function argument
• Class references can be returned from Functions
• Object is a composite entity
• Do not apply any arithmetic and logical operation on object
name directly.

You might also like