Wednesday, 27 July 2016

PASS OBJECT AS PARAMETER

PASS OBJECT AS PARAMETER BY AAMIR JAMIL

OBJECTIVE

Study Pass Objects As Parameter To Methods and Constructors.

THEORY

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;                            //made by aamirjamil
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[]) {
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;                                                 //made by aamirjamil
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);
}
}

The output from this program is shown here:










Passed By Reference

class Test {
int a, b;
Test(int i, int j) {
a = i;                                                  //made by aamirjamil
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;                                                     //made by aamirjamil
Test(int i) {
a = i;
}
Test incrByTen() {
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

No comments:

Post a Comment