aspose file tools*
The moose likes Java in General and the fly likes Passing primitives as References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing primitives as References" Watch "Passing primitives as References" New topic
Author

Passing primitives as References

Madhur Baheti
Greenhorn

Joined: Nov 30, 2000
Posts: 12
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

Joined: Mar 16, 2001
Posts: 985

Ya, use C++.


Please ignore post, I have no idea what I am talking about.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
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

Joined: Nov 30, 2000
Posts: 12
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

Joined: Dec 12, 2002
Posts: 580
You could create a class :

this will achieve want you want and is nice and readable.
T.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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

Joined: Jun 27, 2000
Posts: 286
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing primitives as References