This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Passing primitive types by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing primitive types by reference" Watch "Passing primitive types by reference" New topic
Author

Passing primitive types by reference

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

just a thought Is there any way by which i can pass primitive types by reference what do most most of the java gurus do to accomplish this??


Don’t look where you fall, but where you slipped
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Adam Zedan wrote:just a thought Is there any way by which i can pass primitive types by reference what do most most of the java gurus do to accomplish this??


No. In Java, references only points to objects. And primitives are not objects.

You can achieve this by having objects that contain primitives -- such as arrays of primatives or instances of classes that has instance variables of primative type.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You can't. If you want something like that, create a class that wraps an primitive and use methods to change it. Or use a static variable but I don't recommend that since you can't control access to it.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

There is no such thing as passing by reference, not even for objects. With objects, you pass a copy of the reference. This allows you to modify the object itself, but not the original reference itself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Put them in an array and pass (the value of the reference to) that.

But trying to pass primitives by reference is a weird thing to do - you can probably achieve what you want in a better way, if you explain what you're trying to achieve.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3578
    
  14

I agree with Luigi. There's never a good reason to do this. A much more robust way is to call a method that returns a primitive value based on the primitive you passed to it. The caller can then reassign the variable passed with the value returned. Avoid methods with side-effects if you can.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing primitive types by reference
 
Similar Threads
== comparision
Atomic operation
Is Java Pure Object Oriented?
Please brief on JavaScript architecture
JLS understanding...