Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wanted: How to return multiple values when using java.lang.reflection.invoke?

 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a little function that solves the following equation for real values only (as opposed to imaginary):

a*x^2 + b*x + c = 0;

My attempt is working, but I am not successfully passing back the values r1 and r2 to the calling function.

Is this possible in java? I thought everything except primitives were passed by reference in java!
Thanks
Siegfried
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't say what r1 and r2 are, and I'm not clear on what reflection has to do with anything, nor how pass-by-reference is related to this. This sounds like a case where you've built yourself up a nice big hairy mudball in your brain!

I can answer what I think is the root question here. A Java method can only return one value -- one primitive or one object reference. If you need to return two numbers from a method, then you need to bundle them up together somehow. Return them as a two-element array, or define a class to hold them and return an instance of that.
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a special class that simply holds the two values:

An instance of that whis would be the object reference to return from your method.

[ September 28, 2006: Message edited by: Jean-Francois Briere ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW everything in Java is pass-by-value, primitives and object references.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic