Passing of primitive types and objects to a method

Passing of primitive types and objects to a method


Posted in : Core Java Posted on : October 9, 2010 at 5:02 PM Comments : [ 0 ]

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

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics