1

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.

7
  • Your theory is correct. But Strings are normal objects. Please give one example of Strings behaving like ints. Commented Nov 7, 2014 at 8:49
  • Strings are objects, but they're also immutable, so it's not possible to change a String. Commented Nov 7, 2014 at 8:52
  • 1
    BTW, don't sacrifice clean code for performance unless you know you have a performance problem there. You gain next to nothing on performance by not using getter/setter, but you lose a lot of flexibility. Commented Nov 7, 2014 at 8:56
  • I tried: String string = "hello". Passed it to a method: stringChanger(String string2){ string2 = new String "popo";} when I return from the method and print string it says "hello", thus behaving like if I'd passed an int. It's unchanged. Sorry if I was unclear. Commented Nov 7, 2014 at 8:59
  • possible duplicate of Is Java "pass-by-reference" or "pass-by-value"? Commented Nov 7, 2014 at 9:00

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"?

1 Comment

"Java passes objects" "Objects" are not values in Java (there are no "object types") and cannot be "passed".
0

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.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.