This section describes about the Passing of primitive types and objects to a method in java.
Passing of primitive types and objects to a method
For methods basics and passing primitive data types to methods Click Here
Passing Objects to a Method
In Java , we can pass objects as arguments to a method. Consider the following code :
class ObjDemo {
int a, b;
ObjDemo(int i, int j) {
a = i;
b = j;
}
// return true if o is eaual to the invoking object
boolean equals(ObjDemo o) {
if (o.a == a && o.b == b)
return true;
return false;
}
}
public class PassObjDemo {
public static void main(String args[]) {
ObjDemo ob1 = new ObjDemo(100, 22);
ObjDemo ob2 = new ObjDemo(100, 22);
ObjDemo ob3 = new ObjDemo(-1, -1);
System.out.println("ob1==ob2:" + ob1.equals(ob2));
System.out.println("ob1==ob3:" + ob1.equals(ob3));
}
}
Output :
|
C:\Program Files\Java\jdk1.6.0_18\bin>javac PassObjDemo.java C:\Program Files\Java\jdk1.6.0_18\bin>java PassObjDemo ob1==ob2:true ob1==ob3:false |
Download Source Code

[ 0 ] Comments