Help coderanch get a
new server
by contributing to the fundraiser
  • 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

returning mutiple values

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How do I return two values in a method.
I'm trying to write
public (String aa, int bb) hello(){
.
.
.
return(xx, YY)
}
and it doesn't work
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can add it to some kind of collection (arrayList for example) and then return your collection.
Bosun
 
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
You can pass in a class which contains the values you want to work on, than you wouldn't have to return anything - the values in the class will have been changed.
(You are only allowed to return one thing from a method.)
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If you want to return multiple value from a method.
return it as an Object array...

Object[] func() {
// creting this array put the values and then return it.
Object o[]=new Object[2];
o[0]="hello";
o[1]=new Integer(456);
return o;
}
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
let me first tell u some basic principles about programming languages..
programming languages support 2 types of sub-programs , that can be called from a main program.
these types are..
1)Sub-programs that can return only one value ( primitive or object ). The values returned are generally assigned to a variable in the calling program. These type of sub-programs are generally calles functions..
2)Sub-programs that can return more than one values or none.
Older languages like Pascal supported both the types of sub-programs. But languages like C, C++ and many other new languages support only the first type.
So there is no way u can return multiple values from a function. If u have to return u will first have to construct some type of object as advised in other answers and return that single object/structure etc..
kaustubh.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any object you pass as an argument to a function is by reference.
So int i hello(String aa) would mean that if hello made changes in string aa the calling program could see the changes made.
That doesnot work for int, but it would for Integer and anything else that requires a new.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, not true.
String, Integer, all the other Number classes and possibly others are immutable classes. Any attempt to change a String object will result in a new String object being created and refernced as a local variable inside the method, the reference will not be passed out. (hence the existance of the StringBuffer class)
So while it is usually true that changes to a class made within a method are propogated, it is not true for the classes listed above.
Dave.
 
wessel zaalberg
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I learned something new.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I have done in the past is if you have to return more than one primitive value I pass a reference to a Vector or an Object array into the method, wrap the return values in an instance of their wrapper classes and return no value from the method. After the method exists, pull the wrapped values from the Vector or array and get your return values out.
String a = "Hello";
int b = 1;
Vector v = new Vector();
hello(a, b, v);
a = (String)v.elementAt(0);
Integer iVal = (Integer)v.elementAt(1);
b = iVal.intValue();

public void hello(String aa, int bb, Vector vv){
String xx = aa + "There";
int yy = bb++;
Integer bVal = new Integer(yy);
vv.add(xx);
vv.add(bVal);
}
Hope this helps!

------------------
Brian Hoff
Sun Certified Programmer for the Java� 2 Platform
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic