-1

I realize this is a commonly asked question, but here it is ..

I tried to write this to figure out how Java handles parameter passing and so on..

public class CallByValue {

    int key;

    public void changeValue(CallByValue c){
        System.out.println(c);
        c.key=7;
    }

    public void changeValue(int x){
        x=0;
    }

    public static void main(String[] args){
        CallByValue c=new CallByValue();
        c.key=5;
        System.out.println(c);
        c.changeValue(c);
        System.out.println(c.key);
        int x=8;
        c.changeValue(x);
        System.out.println(x);
    }
}

Here I can change the value of a primitive inside an object passed to a method, but I cannot change the value of a primitive passed into a method. Is there a reason why.

user3386479
  • 51
  • 2
  • 8

2 Answers2

1

Primitive types are passed by value, that is copied in the stack, while Objects are passed by copy of pointers.

robermann
  • 1,722
  • 10
  • 19
  • Not just primitives are passed by value. Others are references and are passed by value too (a copy of the reference is passed by value). – peter.petrov Feb 26 '14 at 23:53
  • In java doesn't exist pointers, they are references – nachokk Feb 26 '14 at 23:54
  • @nachokk Call them as you like, they are addresses in the heap memory :) – robermann Feb 26 '14 at 23:55
  • ok i got it .. i think .. – user3386479 Feb 26 '14 at 23:56
  • @robermann AFAIK references are stored in stack – nachokk Feb 26 '14 at 23:58
  • @robermann Conceptually, they aren't. And as far as implementation details are concerned, some 64 bit JVMs use 32 bit references to "compress pointers". The resulting values aren't pointers or addresses as commonly understood, but perfectly match the concept of a reference, which is just "something which identifies an object". –  Feb 26 '14 at 23:59
  • If you think that a "reference" is an easier metaphor for a Java newbie for me is OK :) – robermann Feb 27 '14 at 00:01
  • @nachokk the references stored in the stack point to positions in the heap – robermann Feb 27 '14 at 00:03
  • @nachokk pointers exist in Java - they are even mentioned in the language specification - they just happen to be usually called references... – assylias Feb 27 '14 at 00:03
  • @assylias mmm.. but if they were pointers you could do things that pointers do for example pointer arithmetic – nachokk Feb 27 '14 at 00:08
  • 1
    @nachokk a pointer is what it says: something that points (or refers) to an address in memory. Whether you can do arithmetic operations on them or not is a different story! – assylias Feb 27 '14 at 00:10
  • 2
    For reference (no pun intended!): http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3.1 "*The reference values (often just references) are pointers to these objects*". But I get your point (!) that a pointer could be a lot more than just a reference. – assylias Feb 27 '14 at 00:15
  • 1
    @assylias i like to learn, thanks. but that word pointer is different in what i mean by pointer perhaps..as you already noticed – nachokk Feb 27 '14 at 00:23
1

Primitive data types are stored in variables by value

(almost) Everything else is stored in variables with reference to associated object

If you pass them into the parameter, the value is always copied.

Therefore primitive data type copy the value and others copy the reference of associated object.

(note that there are abominations like String, but in almost all cases it is as I said)

libik
  • 22,239
  • 9
  • 44
  • 87
  • 2
    What about String makes it special, let alone an "abomination"? It is an ordinary class, and hence a reference type like any other class. –  Feb 26 '14 at 23:58
  • _Italics_ `!=` `code`. – SimonT Feb 27 '14 at 00:02
  • Because String is final, it cannot be changed. Yes, it copy reference like for any other class, but it is really confusing for beginners, when you try to "do something with it" because when you try to append something, new instance is created and stored into that. – libik Feb 27 '14 at 00:05
  • 2
    @libik `final` on classes (as opposed to fields, variables, etc.) is about adding subclasses, not changing anything. It's true that many people are confused about "changing" strings, but that it simply a symptom of a more general confusion about references vs. instance state, which is not String's fault and would bite those people even if String was mutable etc. –  Feb 27 '14 at 00:15