-6

I've got two examples I don't understand

Java passes the value as a variable or passes by reference

Why in the Ref class the integer variable is not changed (null)? Why in the RefCol class the collection variable is modified col(1)?

class Ref:

test(): entero: 5

inicio(): entero: null

class RefCol:

test(): col: [1]

inicio(): col: [1] 

.

import java.util.Collection;
import java.util.Vector;


public class Ref {

    public static void main(String[] args){
        Ref ref = new Ref();
        ref.inicio();
    }

    public void inicio(){
        Integer entero = null;
        test(entero);
        System.out.println("inicio(): entero: " + entero);
    }

    public void test(Integer entero){
        entero = new Integer(5);
        System.out.println("test(): entero: " + entero);
    }

}





public class RefCol {

    public static void main(String[] args){
        RefCol ref = new RefCol();
        ref.inicio();
    }

    public void inicio(){
        Collection col = new Vector();
        test(col);
        System.out.println("inicio(): col: " + col);
    }

    public void test(Collection col){
        col.add( new Integer(1) );
        System.out.println("test(): col: " + col);
    }

}
knowbody
  • 8,106
  • 6
  • 45
  • 70
Marquake
  • 191
  • 1
  • 3
  • 10
  • 2
    http://stackoverflow.com/questions/2208943/how-to-increment-a-class-integer-references-value-in-java-from-another-method – BobTheBuilder Feb 27 '13 at 10:52
  • is not duplicated. I compare Integer vs Collection... ¬¬ – Marquake Feb 27 '13 at 15:21
  • The question referenced holds the answer to your question. The concepts remain the same regardless of which type of object you use – cowls Feb 27 '13 at 15:53

4 Answers4

3

You are passing a COPY of the REFERENCE to an object instance.

If you change the object directly. e.g. col.add it will change the underlying object.

If you change the object it is referencing. E.g. new Integer() it will only change the reference for the local variable.

cowls
  • 24,013
  • 8
  • 48
  • 78
3

Not the same thing.

entero = new Integer(5);

changes the reference entero, while

col.add(new Integer(1));

changes the referenced object col.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
-2

In short: primitive types and "Primitive wrappers (Integer, Long, Short, Double, Float, Character, Byte, Boolean)" can not be altered via reference.

Check http://en.wikipedia.org/wiki/Immutable_object for Details

Kaffee
  • 1,563
  • 11
  • 21
  • this is my favorite answer: D is what I needed to know. Thanks!! – Marquake Feb 27 '13 at 11:18
  • Primitive types and "Primitive wrappers"... Are generally all classes final? I'm triying with StringBuffer(is final) and not change. – Marquake Feb 27 '13 at 11:26
  • This is only half the story. Because these objects are immutable you are creating a new instance insteead of calling a method on them, and hence reassigning the objexct the variable is referencing. If you did col = new ArrayList() you would see the same behaviour – cowls Feb 27 '13 at 11:38
  • 1
    Actually this is NOT a valid answer. He's not trying to change an immutable object anywhere in his code. – m0skit0 Feb 27 '13 at 15:49
  • 1
    Mutability has nothing to do with this question. Assigning a reference of any type will never affect the object referenced, no matter what kind of object. The only way to "mutate" an object pointed to by a reference is to call a method on the object, like he is doing with `add`. He is not doing that with the `Integer` (well, it happens that there are no methods on `Integer` that changes it; but that is besides the point), so that is why it is not the same. – newacct Feb 27 '13 at 19:30
-4

{ objects created here will be destroyed after the ending of brace's(})

}