This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing primitives as References

 
Madhur Baheti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't there any way to pass simple data types as references.I've come cross few options but none seem to be satisfactory
1. Declare the variables in a separate class and then include its object in the main class where the varibles were intended to be. Then pass the object.
2. Instead of declaring simple variables, create a single element array of the same type.
3. Use Wrappers. However, no basic operations can be applied like assignment of an integer, logical operators etc. I find it to be useless.
Is there any real nice way in which i can pass the primitive type variables as references so that any changes in the method reflect in the caller.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya, use C++.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya, use C++.

Translation: you are not thinking in Java.
Perhaps if you described what it is you are trying to accomplish, someone could point out the appropriate Java patterns to use to accomplish it.
hth,
bear
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put them in array. Arrays are objects, so any changes in their contents will be reflected in the object reference in the caller.
see ?
Tom.
 
Madhur Baheti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i am trying to accomplish by passing integers as references is to implement a simple MaxMin algorithm which finds the both max and min no in an array.It involves recursive calls which require that the intger variables passed to the subsequent recursive instance get changed in the original caller.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a class :

this will achieve want you want and is nice and readable.
T.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhur Baheti:
What i am trying to accomplish by passing integers as references is to implement a simple MaxMin algorithm which finds the both max and min no in an array.It involves recursive calls which require that the intger variables passed to the subsequent recursive instance get changed in the original caller.


I don't see why this requires recursion. You can find either the min or the max or both in a single pass over the array, O(n). Unless the array is in some known order you'll need to examine all of the elements anyway so you'll never get an algorithm asymptotically better.
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm maybe Im not reading this right, but what about a public static variable for max and min? They are fully accessible to everyone and there's only 1 instance of them. There's no passing them anywhere.
HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic