This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Integer : pass by value ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer : pass by value ?" Watch "Integer : pass by value ?" New topic
Author

Integer : pass by value ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I have tested pass-by-value and pass-by-reference , found the Integer will be passed by value ? why ? it is because Integer is wrapper class of primitive type ?

Thanks.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, it's much simpler than that. It's because all parameters are always passed by value in Java. You can read this Ranch campfire story which explains why that is.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
Lots of people get very confused about that. There are lots of explanations in this thread
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Paul Clapham wrote:No, it's much simpler than that. It's because all parameters are always passed by value in Java. You can read this Ranch campfire story which explains why that is.


Thanks. Lots of discussions.

as said: "For references to objects, you pass a copy of the reference (the remote control)", reference is the address of the object, so we can say pass by object (reference), On the other side, reference is address, (0x1234), still is a value, so we can say we are passing a value. From this point of view, no any different.

Another question, Integer is an immutable class, like String ?

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Edward Chen wrote:
Another question, Integer is an immutable class, like String ?


Yes. The Javadoc says so.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
Are you sure, Ernest; I can't find "immutable" in the documentation. It says "immutable" in BigInteger, however, for example.
 
Don't get me started about those stupid light bulbs.
 
subject: Integer : pass by value ?