user4179961
user4179961

Reputation:

passing objects as parameters in java

I'm trying to make a 2d game in Java.

In the process I soon noticed that many objects needed bounds (x1, x2, y1, y2) and as a consequence many methods thus had parameters (int x1, int x2, int y1, int y2).

So I thought what the heck, why not make an object IntRectangle with attributes x1, x2, y1, y2 (which I set as public for a performance boost), along with a few helpful methods like getWidth(), getHeight(), etc.

At this point my code is much cleaner and I haven't lost much performance since the variables are public.

I soon noticed, unlike passing primitive values into a method, the values of the passed object changed when I changed them in the method.

For instance, with a primitive value

 int x = 10;

 subtract5(x);
 print(x);

 void subtract5(int i) {
     i -= 5;
 }

this line of code would print 10. But with an object...

class XY {
    public int X;
    public int Y;
}

main {
    XY xy = new XY();

    xy.X = 5;
    xy.Y = 10;

    changeX(xy);

    print xy.X;   
}

changeX(XY xy2) {
    xy2.X = 7;
}

It prints 7;

The theory I've developed is that an int is not a pointer to an int, it's an int stored in the register. It is pushed when a method is called and then popped back when returned, unchanged. An object is a reference to an object. The object itself isn't pushed onto the stack, just the pointer.

But I've tired this with strings (that I know are objects) and they behave just like ints in this matter, which confuses me. Perhaps they make a copy out of themselves somehow?

Well, I hope someone can shed some light on this matter and tell me where my reasoning is taking a wrong turn and hopefully give me some advice on how to clean up my code, keep performance, but without making it venerable.

Upvotes: 1

Views: 7193

Answers (4)

morpheus05
morpheus05

Reputation: 4872

A object in java is a block of memory in the heap space. All you pass around are references to this object/memory area. (The references itself are passed by value) So if you have an Object Rectangle with four ints your methos will change the memory of the heap ara which is references by the passed object reference.

Strings are immutable - so the object cannot change and every modifier method returns a new instance.

See http://javadude.com/articles/passbyvalue.htm for more information.

Upvotes: 2

user4179961
user4179961

Reputation:

Ok, I might have been a bit vague, but the question was not how java passes variables. I just came from that thread. I've figured that out, it was just the String class that was confusing me. And the eventual performance gain of having public attributes. I'll make another attempt, thanks for all your answers!

Upvotes: 0

Navish Sharma
Navish Sharma

Reputation: 243

Strings are constant; their values cannot be changed after they are created.If you change the value of a String it will create a new String object and return a reference to the new String object i.e a new instance is created .E.g

String str="abc";
str=str+"d";

In this case a new String object is created in the String pool and it is now referred by str.The old string is garbage collected.

Upvotes: 0

davidbug
davidbug

Reputation: 41

The fact is that Java is always pass-by-value. In particular Java passes objects as references and those references are passed by value. In case of primitive type, it is passed directly by value.

Further details in the following question:

Is Java "pass-by-reference" or "pass-by-value"?

Upvotes: 0

Related Questions