Skip to Main Content

Question about reference Concept

922984Mar 9 2012 — edited Jun 1 2012
I am studing for the OCJA and I was reading that when you send primitive parameters to a method, you have a copy of them, that means that the original value, won't be change when you come back from the method, but, when you sent objects insted of primitive values, they work by reference, thet means the value from the object will be afected during the method process

So, I tryied to send a Method with a Integer value instead of int, because I know Integer is used like a object, but the value was not afected at the end of the method

the question is... Why a Integer value is not working like a reference value?, it is working like a primitive value
Thank you for your help

This is the code:

Integer valor = new Integer(1);
System.out.println("Argument: value = " + valor);
System.out.println("After method call: value = " + valor);


private static void addfour(Integer valor){
Integer valor4 = new Integer(4);
System.out.println("Parameter: value = " + valor);
valor = (valor + valor4);
System.out.println("Leaving method: value = " + valor);

The output is :
Argument: value = 1
Parameter: value = 1
Leaving method: value = 5
After method call: value = 1
This post has been answered by EJP on Mar 10 2012
Jump to Answer
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Jun 29 2012
Added on Mar 9 2012