• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Passing primitives as References

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, use C++.
 
Sheriff
Posts: 67644
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create a class :

this will achieve want you want and is nice and readable.
T.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic