Archived

This topic is now archived and is closed to further replies.

Cedric

[java] Reference to integer types

Recommended Posts

Cedric    158
I have a function that must return three values. Obviously, void f(int A, int B, int C){...} won''t work. Is there any way to make them references? I know that I could wrap them inside an object, but it''s pretty annoying. Thank you, Cédric

Share this post


Link to post
Share on other sites
misterX    144
Let A,B,C be an array ABC[] and it'll work.

class MyClass
{
...
void modify (int[] ABC) {...}
...
}


Despite of this, making it as an object is the best way to do it.
Sorry to say that, but you must learn thinking in objects.

Depending of what exactly you aim is, different approaches can fit. Here are the 2 main cases:

1. If you want to change 3 values based on few or no parameters

class Data
{
int A;
int B;
int C;
public void modify() {...}
}

class MyClass
{
...
data.modify();
...
}


2. If you want to get 3 "new" values based on few or no parameters

class Data
{
int A;
int B;
int C;
public Data(...) {...}
}

class MyClass
{
...
Data data = new Data(...);
...
}


cheers



[edited by - misterX on October 19, 2002 6:42:16 PM]

Share this post


Link to post
Share on other sites
MaximuS_X    122
Well, you don''t have to make a separte class, simply have it return the 3 integer values inside an array. Then just grab each integer from inside the array.

int[] array = new int[3];
.
.
.
array[0] = A;
array[1] = B;
array[2] = C;
.
.
.
return array;

Share this post


Link to post
Share on other sites
Alimonster    185
Excuse me for missing the point, but...


  
#include <iostream>

using namespace std;

void someFunc(int& a, int& b, int& c)
{
a = 1;
b = 2;
c = 3;
}

int main()
{
int x = 0;
int y = 0;
int z = 0;

someFunc(x, y, z);

cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;

return 0;
}


??

Share this post


Link to post
Share on other sites
MaximuS_X    122
example:

I want a method that returns 3 integer values, but JAVA only allows one return in a method. So, I have to store my 3 integer values inside an array.


    
public int[] return3Ints()
{
int A = 1;
int B = 2;
int C = 3;

int[] array = new int[3];

array[0] = A;
array[1] = B;
array[2] = C;

return array;
}


This is much easier than having to code a new class that holds 3 integer values.

[edited by - MaximuS_X on October 19, 2002 6:55:06 PM]

[edited by - MaximuS_X on October 19, 2002 6:57:04 PM]

[edited by - MaximuS_X on October 19, 2002 8:10:08 PM]

Share this post


Link to post
Share on other sites
Alimonster    185
Uh, to atone for that, here''s something.


  
public void someFunc(int[] myArray)
{
for (int i = 0; i < myArray.length; ++i)
myArray[i] = i + 1;
}

public void doSomething() {
int[] myArray = new int[3];

myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 0;

someFunc(myArray);

for (int i = 0; i < myArray.length; ++i) {
System.out.println("The number is: " + myArray[i]);
}
}


Dammit, Cédric, you posted here just to trick me, admit it! Stick to C++ in future .

Share this post


Link to post
Share on other sites
Cedric    158
quote:
Original post by Alimonster
Dammit, Cédric, you posted here just to trick me, admit it! Stick to C++ in future .

That''ll teach you to use the Active Topics page! Lazy GameDever!

Thanks for the response, everyone. I already knew about the array work-around, but I don''t find it very elegant. "Thinking in objects" sounds more like "Living with only one arm and one leg".

I miss C++ already

Cédric

Share this post


Link to post
Share on other sites
misterX    144
quote:
Original post by cedricl
"Thinking in objects" sounds more like "Living with only one arm and one leg".

I miss C++ already



Thinking in objects has become my second nature and i'm now very comfortable with it. If you aren't affraid and not so oriented on "coding", you'll see OO is quite intuitive in fact, it's really like a mirror of a real world.
It's not "missing one arm and one leg" but more have them at the right place!

...You shouldn't miss c++, java has so much to offer, especially it's awefull simplicity!

[edited by - misterX on October 20, 2002 7:04:28 AM]

Share this post


Link to post
Share on other sites
Cedric    158
quote:
Original post by Neophyte
You could use Integer''s, of course.

Ah! That, I did not remember. Unfortunately, it''s not as elegant as using an int reference in C++, but it''s still a solution worthy of consideration. Thank you.

Cédric

Share this post


Link to post
Share on other sites
HenryApe    120
java.lang.Integer is an immutable class so you can not change its value to pass stuff back.

Output parameters passed as input parameters can be quite a convenient little hack, but I would definitely not call them elegant. For elegance and code clarity, input parameters should strictly be used to provide input to functions and return parameters should be used to return values. Hopefully they can implement escape analysis in Sun''s JVM soon and take away the performance penalty for creating composite return parameter objects.

Share this post


Link to post
Share on other sites
Viro    122
What''s escape anaylsis in layman''s terms? A search on Google turned up a lot of some really deep stuff that''s way over my head.

Share this post


Link to post
Share on other sites
HenryApe    120
The main use of escape analysis here would be to let the JVM determine which objects can be allocated on the stack rather than the heap. The big reason why allocation is so slow in Java is that all objects are allocated on the heap and must be deallocated by the garbage collector.

Share this post


Link to post
Share on other sites
Argus    118
Remember you can always use nested classes to make things a bit nicer :

public class MainStuff {

...

private class Color {
int redVal;
int greenVal;
int blueVal;
}

}
Since with member classes there is a lot less compulsion to use encapsulation. It''s the best workaround that I''ve found without making things ugly.

Share this post


Link to post
Share on other sites
Neophyte    595
quote:

java.lang.Integer is an immutable class so you can not change its value to pass stuff back.


You''re quite right, HenryApy. My apologies to all concerned.

Speaks volumes that someone who''s been a professional java-programmer for 2 years (i.e. me) doesn''t know that Integer''s are immutable. The only time I really use the wrapper-objects for primitive datatypes are when I need to put them in some collection or other.

-Neophyte

Share this post


Link to post
Share on other sites