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.

helvete
  • 2,455
  • 13
  • 33
  • 37
Jake
  • 843
  • 1
  • 7
  • 18
  • Your theory is correct. But Strings are normal objects. Please give one example of Strings behaving like ints. – user253751 Nov 07 '14 at 08:49
  • Strings are objects, but they're also immutable, so it's not possible to change a String. – Robby Cornelissen Nov 07 '14 at 08: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. – Thomas Stets Nov 07 '14 at 08: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. – Jake Nov 07 '14 at 08:59
  • possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – ToYonos Nov 07 '14 at 09:00
  • So there is no gain with public attributes? I'm thinking that if I'm in a method that has 200 attributes and performs a loop 5000 times. In this loop I do a get method to access a variable. That means 200 variables has to be pushed and popped 5000 times every time I call the method. That's quite a few instructions wasted. Or have I misunderstood something? – Jake Nov 07 '14 at 09:04
  • @Jake I'm pretty sure you misunderstood something. The local variables of a method exist on the stack. They do not get pushed there when the method makes a call to another method. The only things being pushed on the stack are the parameters of the method to call, and the return address. (And I hope your example was just an exaggeration. If you really have a method with 200 variables I suspect you have some serious design problems) – Thomas Stets Nov 07 '14 at 09:26

4 Answers4

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.

morpheus05
  • 4,772
  • 2
  • 32
  • 47
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"?

Community
  • 1
  • 1
davidbug
  • 41
  • 3
  • "Java passes objects" "Objects" are not values in Java (there are no "object types") and cannot be "passed". – newacct Nov 07 '14 at 19:42
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.

Navish Sharma
  • 233
  • 2
  • 11
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!

Jake
  • 843
  • 1
  • 7
  • 18