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

Lab # 7 Pass Object As Parameter: Objective

Uploaded by

Hasnain Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Lab # 7 Pass Object As Parameter: Objective

Uploaded by

Hasnain Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Object as Parameter Lab # 7

LAB # 7

PASS OBJECT AS PARAMETER

OBJECTIVE:
To Study Pass Objects As Parameter To Methods.

THEORY:

When we pass a primitive type to a method, it is passed by value. But when


we pass an object to a method, the situation changes dramatically, because
objects are passed by what is effectively call-by-reference. Java does this
interesting thing that’s sort of a hybrid between pass-by-value and pass-by-
reference. Basically, a parameter cannot be changed by the function, but the
function can ask the parameter to change itself via calling some method within
it.

7.1 PASSING OBJECTS TO A METHOD:

When you pass an object as an argument to a method, the mechanism that


applies is called pass by reference, because a copy of the reference
contained in the variable is transferred to the method, not a copy of the object
itself. The effect of this is shown in Figure:

 While creating a variable of a class type, we only create a


reference to an object. Thus, when we pass this reference to a
method, the parameter that receives it will refer to the same object
as that referred to by the argument.
 This effectively means that objects act as if they are passed to
methods by use of call-by-reference.
 Changes to the object inside the method do reflect in the object
used as an argument.

Object Oriented Programming - OOPs 1


Object as Parameter Lab # 7

The Lifetime of an Object:

The lifetime of an object is determined by the variable that holds the


reference to assuming there is only one. If you have the declaration

then the Sphere object that the variable ball refers to will die when the
variable ball goes out of scope. This will be at the end of the block containing
this declaration. Where an instance variable is the only one referencing an
object, the object survives as long as the instance variable owning the object
survives.

In previous lab we have only been using simple types as parameters to


methods. However, it is both correct and common to pass objects to methods.
For example, consider the following short program:

Objects may be passed to methods


class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// return true if o is equal to the invoking object
boolean equals(Test o) {
if(o.a == a && o.b == b) return true;
else return false;
}
}
class PassOb {
public static void main(String args[]) {

Object Oriented Programming - OOPs 2


Object as Parameter Lab # 7

Test ob1 = new Test(100, 22);


Test ob2 = new Test(100, 22);
Test ob3 = new Test(2,1);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}
This program generates the following output:

As you can see, the equals( ) method inside Test compares two objects for
equality and returns the result. That is, it compares the invoking object with
the one that it is passed. If they contain the same values, then the method
returns true. Otherwise, it returns false. Notice that the parameter o in
equals( ) specifies Test as its type. Although Test is a class type created by
the program, it is used in just the same way as Java’s built-in types.

A Closer Look at Argument Passing:

In general, there are two ways that a computer language can pass an
argument to a subroutine. The first way is call-by-value, the second way an
argument can be passed is call-by-reference.

Passed By Value
class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " +
a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}

Object Oriented Programming - OOPs 3


Object as Parameter Lab # 7

The output from this program is shown here:

Passed By Reference
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
This program generates the following output:

Returning Objects:
A method can return any type of data, including class types that you create.
For example, in the following program, the incrByTen( ) method returns an
object in which the value of a is ten greater than it is in the invoking object.

Returning an object
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {

Object Oriented Programming - OOPs 4


Object as Parameter Lab # 7

Test temp = new Test(a+10);


return temp;
}
}
class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);
}
}

The output generated by this program is shown here:

As you can see, each time incrByTen( ) is invoked, a new object is created,
and a reference to it is returned to the calling routine

LAB TASK
1. Write a program to take two values from the user and swap those
values by using the concept of arguments passing by reference.

Sample Output:
Before swapping:
First value: 15
Second value: 20
After Swapping:
First value: 20
Second value: 15

2. Write a program to take a value from the user and print next 5 values
that should be the increment of 10 from the previous value using the
concept of returning object

Object Oriented Programming - OOPs 5


Object as Parameter Lab # 7

Sample Output:
5 15 25 35 45

3. Write a program to calculate time for 2 different locations by using the


concept of arguments passing by value.
Sample Output:
Time: hour: mins: sec
Time1: 3:30: 5
Time2: 4:45:15

Object Oriented Programming - OOPs 6

You might also like