• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing primitive types by reference

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic