I am once again experimenting and Only to find out that java is pass by value,

how do I actually manipulate a method or an array that has been pass to another method?

for example

int[]a ={1,2,3,4,5,6};
modify(a);
________________________

void modify(int[] aTest){
aTest[1]=2;
}

//take note that those arrays are from a different files/class

now the thing is how would I manipulate the reference/original array? I am almost getting the topic but I just need clarification.

Recommended Answers

All 23 Replies

Pass by reference: wrong. All Java calls are pass by value, even when the value being passed is a reference.

Your code will work. Arrays are Objects, and Objects are passed to methods by passing a copy of a reference to the Object. So your parameter aTest contains a copy of the reference value that was in the variable a. It refers to the same original Object (array) and any code that modifies the object or calls a method on it will modify/call the original object.

Your code will work. Arrays are Objects, and Objects are passed to methods by passing a copy of a reference to the Object. So your parameter aTest contains a copy of the reference value that was in the variable a. It refers to the same original Object (array) and any code that modifies the object or calls a method on it will modify/call the original object.

soo let me get this straight , if I pass an object array in a method, then manipulate the values in it, the original object will be change? am I correct?

Yes

Yes

do you have any link about that for further reading? not that I am not satisfied with your answer I just more understanding about this

You can Google it - there are some excellent articles on the web. Just be careful to check the source, there is one completely wrong explanation that got copied a few times. If in any doubt go to the Java Language Specification.
Section 8.4.1 has the definitive statement on call by value:

When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor.

Plus, of course, you can always run a tiny test program.

ps: This one is particularly clear:
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

and this is a version of the completely WRONG story that will find copied here and there
http://www.java2s.com/Book/Java/0040__Class/Passbyvalue_vs_Passbyreference.htm
I've only posted this so you know what to look out for.

You can Google it - there are some excellent articles on the web. Just be careful to check the source, there is one completely wrong explanation that got copied a few times. If in any doubt go to the Java Language Specification.
Section 8.4.1 has the definitive statement on call by value:


Plus, of course, you can always run a tiny test program.

ps: This one is particularly clear:
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

and this is a version of the completely WRONG story that will find copied here and there
http://www.java2s.com/Book/Java/0040__Class/Passbyvalue_vs_Passbyreference.htm
I've only posted this so you know what to look out for.

Thanks James, Finally got an Idea how will I manipulate my array of objects, soo to finalize if I pass an array of object as a parameter, if I edit the data of it, the original object will change right?

Yes. Your method will have a (copy of) a reference to the original array or object, and can use that to manipulate it directly.
Try a small test program.

Yes. Your method will have a (copy of) a reference to the original array or object, and can use that to manipulate it directly.
Try a small test program.

Here it is but it's not working

public class TestRun{

		public static void main(String[] args){
			
			Test t1 = new Test();
			Test t2 = new Test();
			t1.x = 10;
			t1.y = 15;
			t2.x= 1;
			t2.y = 23;
			
			System.out.println("t1.x = "+t1.x+" t1.y = "+t1.y);
			System.out.println("t2.x = "+t2.x+"  t2.y = "+t2.y);	
			swap(t1,t2);
			System.out.println("t1.x = "+t1.x+" t1.y = "+t1.y);
			System.out.println("t2.x = "+t2.x+"  t2.y = "+t2.y);
		}
									
		public static void swap(Test x, Test y){
			Test temp = x;
			x = y;
			y = temp;	
		}
}
public class Test{

		int x = 50;
		int y = 10;
}

That swap method is one that only works in a language with pass by reference, so it will not, cannot, work in Java. Java is always and only pass by value.

What you can do, going back to your original question, is update an original instance of Test as in

Test t1 = new Test();
update(t1);
// print t1

void update(Test t) {
   t.x = 999;
   t.y = -42;
}

That swap method is one that only works in a language with pass by reference, so it will not, cannot, work in Java. Java is always and only pass by value.

What you can do, going back to your original question, is update an original instance of Test as in

how about this?

public class TestRun{

		public static void main(String[] args){
			
			Test t1 = new Test();		
			System.out.println("t1.x = "+t1.x+" t1.y = "+t1.y);
			System.out.println("SWAP!");
			swap(t1,t1);
			System.out.println("t1.x = "+t1.x+" t1.y = "+t1.y);
			
		}
									
		public static void swap(Test t1, Test t2){
			int temp = t1.x;
			t1.x = t2.y;
			t1.y = temp;
		}
}
public class Test{

		int x = 9999;
		int y = 1;
}

I don't know what you are trying to achieve, so I can't answer that question.

I don't know what you are trying to achieve, so I can't answer that question.

swapping the values? wait let me show something

You can't perform a swap like that in Java.

I don't know what you are trying to achieve, so I can't answer that question.

swapping the values? of the instance of the object, how about this code??

public class TestRun{

		public static void main(String[] args){
			
			Test t1 = new Test();	
			System.out.println("t1.x = "+t1.x+" t1.y = "+t1.y);
			System.out.println("SWAP!");
			swap(t1,t2);
			System.out.println("t1.x = "+t1.x+" t1.y = "+t1.y);
			
		}
									
		public static void swap(Test x, Test y){
			Test temp = x;
			x = y;
			y = temp;
		}
}

but it's not working ?

Parameters x and y hold COPIES of the values of t1 and t2. Nothing you do in the swap methods will have any effect on the variables t1 and t2. What you CAN do is change things in the objects that t1/x and t2/y refer to.

Parameters x and y hold COPIES of the values of t1 and t2. Nothing you do in the swap methods will have any effect on the variables t1 and t2. What you CAN do is change things in the objects that t1/x and t2/y refer to.

can you provide me an example using my code? I am having a hard time about this, well confused actually, please? thanks in advance

I posted a little example before. You just have to plug that into your class.
It's confusing because you code
Test t1;
and think that t1 is a Test, but it's not, it's a reference to a test. Imagine it's a pointer - an integer holding the address of an instance of Test (not strictly true, but helpful anyway). Ditto your parameters x and y in your swap method. When you call the mthod the address in t1 is copied to x, so you can access the same instance of Test, but you can't change t1 itself.
I don't know how to express this more clearly.
Read this one again
http://www.javaworld.com/javaworld/j...0526-pass.html

Anyway, I'm off to dinner now. Good luck.

I posted a little example before. You just have to plug that into your class.
It's confusing because you code
Test t1;
and think that t1 is a Test, but it's not, it's a reference to a test. Imagine it's a pointer - an integer holding the address of an instance of Test (not strictly true, but helpful anyway). Ditto your parameters x and y in your swap method. When you call the mthod the address in t1 is copied to x, so you can access the same instance of Test, but you can't change t1 itself.
I don't know how to express this more clearly.
Read this one again
http://www.javaworld.com/javaworld/j...0526-pass.html

Anyway, I'm off to dinner now. Good luck.

Here's a simpler code

public class TestRun{
	
		public static void main(String[] args){
			Test t1 = new Test();
			
			System.out.println("The original valuess of X and Y are");
			System.out.println("X =  "+t1.x+"Y = "+t1.y);
	modifyObject(t1);
			System.out.println("After the modification ");
			System.out.println("X =  "+t1.x+"Y = "+t1.y);
			
		}
		
		public static void modifyObject(Test arg1){
			arg1.x = 555;
			arg1.y = 333;
		}
}
public class Test{

		int x = 9999;
		int y = 1;
}

But the thing is it is not updating any idea why?

or this is the right one?

public class TestRun{
	
		public static void main(String[] args){
			Test t1 = new Test();
			
			System.out.println("The original valuess of X and Y are");
			System.out.println("X =  "+t1.x+"Y = "+t1.y);
			modifyObject(t1);
			System.out.println("After the modification ");
			System.out.println("X =  "+t1.x+"Y = "+t1.y);

			
		}
		
		public static void modifyObject(Test arg1){
			Test t2 = new Test();
			t2.x = 555;
			t2.y = 888;
			arg1 = t2;
		}
}

and Here is the other code, which is the right one?

public class TestRun{
	
		public static void main(String[] args){
			Test t1 = new Test();
			Test t2 = new Test();
			t2.x = 555;
			t2.y = 333;
			
			System.out.println("The original valuess of X and Y are");
			System.out.println("X =  "+t1.x+"Y = "+t1.y);
			modifyObject(t1,t2);
			System.out.println("After the modification ");
			System.out.println("X =  "+t1.x+"Y = "+t1.y);

			
		}
		
		public static void modifyObject(Test arg1,Test arg2){
			arg1 = arg2;
		}
}

Your first program is correct and updates as it should.
The second and third are useless because they attempt to update the parameter which has no effect as per all my previous explanations.
I'm away for a couple of days now, so just go back to the info and reference I gave you before. Try drawing the variables, parameters and objects on a sheet of paper and fill in their values as you step thru the code. It's clear you haven't got this straight yet, but I honestly don't know how to make it clearer. Maybe someone else can help???

Your first program is correct and updates as it should.
The second and third are useless because they attempt to update the parameter which has no effect as per all my previous explanations.
I'm away for a couple of days now, so just go back to the info and reference I gave you before. Try drawing the variables, parameters and objects on a sheet of paper and fill in their values as you step thru the code. It's clear you haven't got this straight yet, but I honestly don't know how to make it clearer. Maybe someone else can help???

alright. I already understand about this issue, thanks, sorry for the late updated, tested different programs.

java is not pass by reference...evrything that java does is pass by value.. remember array is an object.. when u create array it actually stores the reference to the array that has been created on the heap..when u pass the array to a method.. it copies the refernce...but not the actual values..even after passing an array to a method it would still point to the same object..
hope u understood

Hi hemanth
I see you are new to DaniWeb. Welcome.
Please check the dates on threads before adding to them. This one is 25 days old, so the OP has either got his answer or given up long ago.
Spend one quick minute reading the DaniWeb policies:
http://www.daniweb.com/forums/faq.php?faq=daniweb_policies
these will help you avoid inappropriate posts.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.