Jump to content
  • Advertisement
Sign in to follow this  
MindWipe

[java] References in java!?

This topic is 5384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I've been coding with java for a while. I'm a professional j2me developer. But being a C/C++ at heart I have some problems. Ok, I understand java does not support pointers and allways sends objects/arrays/?????? as references, but not variables. class MyClass { int X; } public void SetA( MyClass mc ) { mc.x = 10; } In this case if I do: MyClass myclass = new MyClass(); SetA(myclass); then myclass.x equals 10. but if I do: public void SetA( int x ) { x = 10; } int myx; SetA(myx); then myx won't equal 10. Ok, no problem understanding this so far, but my problem is when I want a function to set multiple variables. public void SetAB( int a, int b) in C/C++ I could have used references or pointers. But how do I do this in Java!?? /MindWipe

Share this post


Link to post
Share on other sites
Advertisement
Use the Integer type to pass the value:

Integer ai,bi;
call_function (ai, bi);
int a = ai, b = bi;


Skizz

Share this post


Link to post
Share on other sites
Similarly if you want to pass objects by reference you would have to wrap them.

class Output
{
Object value;
}

...

public void doSomething(Output o1, Output o2)
{
...
}

It's not nice, but it works.. Using java 1.5 you could make it better though by specifying the type of value.

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
Use the Integer type to pass the value:

Integer ai,bi;
call_function (ai, bi);
int a = ai, b = bi;


Skizz

... Which fails because Integers are immutable, and if you assign a new Integer to ai or bi inside call_function, you are only altering what the local references point to.

You could pass an array (Integer[]) with two elements and assign new values to them.

Share this post


Link to post
Share on other sites
I use Thex0's method, and if it's a primitive I further wrap it in it's appropiate class, or make a special IntOutput that has an int instead of an object. And yes, it does looks bad.

shmoove

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by MindWipe
Alright, I've been coding with java for a while. I'm a professional j2me developer. But being a C/C++ at heart I have some problems.


And there is the heart of your problem - you are not programming in java at all here, but in C. Go read Bruce Eckel's "thinking in java" (it's free if you read it online). If you find you know whole sections, skip them; you will probably find quite a lot of useful stuff you hadn't quite stopped to think about before (esepcially if you normally use J2ME instead of J2SE).

Quote:

Ok, I understand java does not support pointers and allways sends objects/arrays/?????? as references, but not variables.


Everything in java is pass-by-reference EXCEPT for "basic datatypes" (int, short, long, boolean, char, byte, float, double - which are unique because they are the ONLY types in java that are java keywords, so they're easy to distinguish), which are pass-by-value.

The real question, though, is what the heck are you TRYING to do? Rather than us telling you how to do a C thing in java, tell us what you are trying to do in terms of programming and we can tell you the best/easiest/fastest way to do that *in java*...

This:
Quote:



class MyClass
{
int X;
}

public void SetA( MyClass mc )
{
mc.x = 10;
}



...is a bastardization of a standard OOP pattern: you appear not to fully understand OOP with that method! The *whole point* of OOP is that you do NOT EVER write methods that take a class and set it's values: those methods MUST be inside the class itself (OK, the compiler doesn't care, and there are instances where you violate this legitimately, but the point of OOP is not to do this...).

So, you ought instead to have a method in MyClass which is:

public void setX( int newx )
{
x = newx;
}

Maybe understanding OOP is all you need, but maybe you're trying to do something more cunning (I'm assuming the former since it's more likely, statistically! Apologies if I've inadvertently patronized you!). If it's the latter, tell us what you are doing in a bigger sense and then we'll be able to help better...

redmilamber (who managed to stay logged in for almost 2 days this time - seems GD.net cookies have been improved - but then GD.net staff broke it again and it is now IMPOSSIBLE for me to login [I get logged out *instantly* after a successful login])

Share this post


Link to post
Share on other sites
Alright. I get it.

Thanks all! Especially you AP!

I solved my "problem" by doing it in a whole other way. I'm still talking about j2me and I want to minimize the amount of classes as they take a bit of the heapmemory.

/MindWipe

Share this post


Link to post
Share on other sites
IIRC, you can pass arrays of primitives by reference, ie

// fill grid with 0's

public void putInArrayPlzKthxBye(int[][] grid) {
// fill grid with 1's
}

// grid should be filled with 1's

... if you couldn't, that would be possibly a lot of memory to copy. AFAIK it works in my current project.

Share this post


Link to post
Share on other sites
Quote:
Original post by temp_ie_cant_thinkof_name
IIRC, you can pass arrays of primitives by reference

... Because arrays are objects.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!