🔒 Locked
[java] call by reference(speed question)
Advertisement
Original Post
There is no Java equivalent for int reference parameters in C++ because integers and other primitive types are passed by value.
int[] in java is not an int pointer like it is in C++; it's an array object passed by reference, most similar to a std::vector by reference but without the complex interface.
For any data type, using 1-element arrays in Java is a waste of memory (for the array object) and time (for range checking and indirect access).
I'm afraid the apples to oranges comparison demonstrates a dangerous misunderstanding both of Java semantics and, as Fred304 noted, of how to optimize code.
int[] in java is not an int pointer like it is in C++; it's an array object passed by reference, most similar to a std::vector
For any data type, using 1-element arrays in Java is a waste of memory (for the array object) and time (for range checking and indirect access).
I'm afraid the apples to oranges comparison demonstrates a dangerous misunderstanding both of Java semantics and, as Fred304 noted, of how to optimize code.
Quote:
Original post by LorenzoGatti
There is no Java equivalent for int reference parameters in C++ because integers and other primitive types are passed by value.
Everything in Java is passed by value. I believe we recently discussed this in General Programming.
Quote:
int[] in java is [...] an array object passed by reference
int[] is a reference to an array object, and this reference gets passed by value. This means you can make changes to the object via the local copy of the reference (in this case the elements of the array), but you cannot change the reference itself because you only have a copy of it.
public void possible(int[] a){ a[0] = 42; // changes the object}public void impossible(int[] a){ a = new int[10]; // changes local copy of a // but does NOT affect original reference}Hope this makes it clear :)
Quote:
Original post by LorenzoGatti
There is no Java equivalent for int reference parameters in C++ because integers and other primitive types are passed by value.
Actually they are passed by register, or as a virtual register pass. (It's in effect the same as saying if the biggest value is 64 bits they are passed by value.)
As for original question, it can't be determined, but
someMethod(int c) would be most surely inlined if possible.
Quote:
Original post by Anonymous Poster Quote:
Original post by Fred304
Everything in Java is passed by value. I believe we recently discussed this in General Programming.
Ok. Could you please elaborate this subject for the beginners?
This should be in some kind of FAQ on the forum, but here's a nice read: JavaRanch Campfire - Pass By Value, Please
Quote:
Original post by Anonymous Poster Quote:
Original post by Fred304
Everything in Java is passed by value.
Ok. Could you please elaborate this subject for the beginners?
Java allocates objects on the heap. A variable of a non-primitive type never holds the object itself but merely a reference to the object.
-------------|some object|------------- ^ | | |-+-| | x---Take a look at the following line of code:
Foo x;In both languages we say: x ist of type Foo. But the semantics are different!
C++: x holds an instance of the class Foo (which gets constructed on this line). You can instantly call methods like x.bar;
Java: x holds a reference to an instance of Foo (or a class somewhere down the inheritance tree). No constructor has been called and x does not reference any object yet. The value of x is null. Immediately calling x.bar() will yield a NullPointerException.
Here's how C++ programmers can translate Java code:
Java C++---------------------Foo x; Foo *x;x.bar(); x->bar();Now when you pass a variable as a parameter into a method, the formal parameter gets a copy of that value. If the parameter is int, then the integer value is copied. If it is a boolean, the boolean value is copied etc. If you change the int, boolean... inside the method, you only change your local copy. It will not affect the variable outside of the method (because the variable is not actually passed in but merely its value).
public void bar(int a){ // before a = 7; // after}public void call(){ int i = 5; bar(i);}before:--- ---|5| i |5| a--- ---after:--- ---|5| i |7| a--- ---Same thing with non-primitive types: the value of the variable gets copied. What is the value of a variable of type Foo? Remember, it's NOT an instance/object of Foo. It's a reference to an object living on the heap! This reference (i.e. 4 bytes) gets copied to the formal parameter. If you change the value of the parameter inside the method (Remember the value of the parameter? An object? NO! A reference to an oject!), then this has absolutely no effect to the variable outside the method.
public void bar(Foo f){ // before f = new Foo("wurst"); // after}public void call(){ Foo x = new Foo("hans"); bar(x);}before:--------|"hans"|-------- ^ ^ | | | +--+ | |-+- -+- | | x | | f--- ---See? Both variable have distinct references which happen to point to the same object.
after:-------- ---------|"hans"| |"wurst"|-------- --------- ^ ^ | | | +--+ | |-+- -+- | | x | | f--- ---Now f points to another object. Changing the value of f (the reference!) did not affect the value of x. x still points to the object pointed to before the method call.
Of course you can change the referenced object (since this is not the value of the original variable) if both variables reference the same object. But you have to remember that these are two distinct variables which are not tied together in any way. This is what pass by value is all about. The value of the variable gets copied, so the method cannot change that value.
public void bar(Foo f){ // before f.set("wurst"); // after}public void call(){ Foo x = new Foo("hans"); bar(x);}before:--------|"hans"|-------- ^ ^ | | | +--+ | |-+- -+- | | x | | f--- ---Both variables hold distinct references to the same object.
after:---------|"wurst"|--------- ^ ^ | | | +--+ | |-+- -+- | | x | | f--- ---Calling methods on f is like calling methods on x. This has the effect that the object referenced by x can change inside the method. But although the object has changed, x itself has not changed. It is still the same reference!
And now I want a cookie for all these horrible ASCII diagrams :)
EDIT: let me be more clear what "x does not change" means:
public void bar(Foo f){ // before f.set("wurst"); // after}public void call(){ Foo x = new Foo("hans"); Foo x_before_call = x; bar(x); assert (x == x_before_call);}Before the call, x points to a certain object containing "hans". After the call, x still points to the same object (only the content of the object has changed from "hans" to "wurst"). The identity of the object is preserved - it is still the same object.
The bottom line is "x is not a Foo - it is a reference to a Foo. Local copies of this reference can change the Foo, but they cannot change the original reference." Repeat this 10 times before going to sleep ;)
Quote:
Original post by Anonymous Poster
I am going to resume your explanation for the beginners in real world terms, ok?
You're welcome to do so. But remember, this is not the beginner's forum, it's the java forum.
Quote:
- You can change the contents of a class you passed to a method but not the reference itself.
You mean object.
Quote:
People usually want to do this:
List list = new List();
populateList(list);
Right.
Quote:
Then populateList will add items to the list, and those items won't be lost at the end because the list wasn't copied to somewhere else.
Right.
Quote:
- You can't change the value of a primitive type because they are always copied when passed to a method.
int i = 5;
calculateWhatever(i);
If calculateWhatever changes the value of i inside of the method it will be lost at the end of the method.
Ok, but as pointed out, the same goes for non-primitive types. You can't change the variable because only its value gets passed to the method. How often have I seen code like this:
Foo f;Bar b;fillMe(f, b);And people wonder why it does not work.
Quote:
Honestly, I think this is the biggest point of confusion specially among "young Java haters", the types that learned how to program last semester and think they are l33t. Older people should know that.
Who exactly are you talking to? I love Java. I am just a little nitpicky when it comes to semantics :)
Quote:
I am really tired of seeing this kind of discussion, that inevitably falls in "my language is better than yours" (not the case here thank God) debate, usually started by a C++ developer pointing out "the defects" (because he doesn't know how to use it, of course) of Java.
This is a discussion about call by value and call by reference. I don't see how it hurts anybody.
I'll tell ya why it hurts.
It's because this fecking forum has just become a total waste of bandwidth for CS students who don't bloody listen in their lectures.
Every post is about some trivial, NON-GAME-RELATED piece of basic Java knowledge. I mean, what is the point of this entire subforum being on Gamedev.net? It's a complete waste of space. The forum might as well be called ididntlisteninlectures.com/community/forums.php or something.
Or, imtoolazytoactuallyreadafuckingbook.com.
Or maybe imsocluelessididntevenbothertousegoogle.com.
Grief.
I hang around in here these days in the vague chance that someone is actually going to ask a game development related question, but alas...
Cas :)
It's because this fecking forum has just become a total waste of bandwidth for CS students who don't bloody listen in their lectures.
Every post is about some trivial, NON-GAME-RELATED piece of basic Java knowledge. I mean, what is the point of this entire subforum being on Gamedev.net? It's a complete waste of space. The forum might as well be called ididntlisteninlectures.com/community/forums.php or something.
Or, imtoolazytoactuallyreadafuckingbook.com.
Or maybe imsocluelessididntevenbothertousegoogle.com.
Grief.
I hang around in here these days in the vague chance that someone is actually going to ask a game development related question, but alas...
Cas :)
Quote:
Original post by Fred304 Quote:
Original post by Anonymous Poster Quote:
Original post by Fred304
Everything in Java is passed by value.
Ok. Could you please elaborate this subject for the beginners?
Java allocates objects on the heap.
This is currently true for all JVMs afaik, but Java6 will most likely use escape analysis to determine if it might be better to allocate certain objects on the stack.
Quote:
Original post by princec
I'll tell ya why it hurts.
It's because this fecking forum has just become a total waste of bandwidth for CS students who don't bloody listen in their lectures.
Do you think the OP of this thread is actually such a person? Did not sound like a student to me.
Quote:
I hang around in here these days in the vague chance that someone is actually going to ask a game development related question, but alas...
I just find it nice to have a subforum for people interested in discussing Java. I recently noticed lots of threads about Java problems in other subforums of gamedev, maybe a mod could move them to this forum if you'd like that? ;)
Quote:
Original post by RayNbow Quote:
Original post by Fred304
Java allocates objects on the heap.
This is currently true for all JVMs afaik, but Java6 will most likely use escape analysis to determine if it might be better to allocate certain objects on the stack.
Right, totally forgot about that. A very interesting and promising feature! Looking forward to it!
Quote:
Original post by Fred304 Quote:
Original post by Anonymous Poster Quote:
Original post by Fred304
Everything in Java is passed by value.
Ok. Could you please elaborate this subject for the beginners?
Same thing with non-primitive types: the value of the variable gets copied. What is the value of a variable of type Foo? Remember, it's NOT an instance/object of Foo. It's a reference to an object living on the heap! This reference (i.e. 4 bytes) gets copied to the formal parameter. If you change the value of the parameter inside the method (Remember the value of the parameter? An object? NO! A reference to an oject!), then this has absolutely no effect to the variable outside the method.
You are trying very hard not to say, that YOU THINK java references are passed by value that does not negate the statement that objects are passed by reference. But objects are passed by reference they are assigned by reference and there is no way to dereference a variable reference to an object thus no need for any arguement that reference variables are passed by value between . and ->
However, it is a possibility that variable references could be passed by name and a separate variable is only created when a variable is assigned a object reference that doesn't already exist thus.
// new variable allocated
Foo x = new Foo();
Foo y = x;
someFunct(y); //passed by name as x or y no new copy needed.
y = new Foo(); // new reference variable created.
pass by name is all done at compile-time. This is a more optimized approach however small it may seem. I haven't actually looked at the JVM source so I don't know if it does it this way but this would be an alternative method to copying the reference everytime it's passed.
Quote:
Original post by Anonymous Poster
Saying that "everything is passed by value" is a half truth (or a half lie depending of you point of view) because it implies that the class is copied to different region in memory and any change to it is lost at the end.
Java object variables are nothing but a pointer to the actual object and these memory addresses are passed by value, as explained by the link I posted earlier in this thread.
I think I just got out the wrong side of bed the other day.
Yes, the OP sounds like a student. Probably GCSE or A-Level standard, and not particularly bright at that judging by the amount of passive research that must have gone into the problem before pestering people in here.
Cas :)
Yes, the OP sounds like a student. Probably GCSE or A-Level standard, and not particularly bright at that judging by the amount of passive research that must have gone into the problem before pestering people in here.
Cas :)
Quote:
Original post by RayNbow Quote:
Original post by Anonymous Poster Quote:
Original post by Fred304
Everything in Java is passed by value. I believe we recently discussed this in General Programming.
Ok. Could you please elaborate this subject for the beginners?
This should be in some kind of FAQ on the forum, but here's a nice read: JavaRanch Campfire - Pass By Value, Please
It is in the freaking forum FAQ. I know because I freaking wrote the freaking entry.
well then maybe it should be a new type of passing parameters to functions since it's not exactly passing by value because you are able to manipulate the object and the whole pointer thing is nonexistant to the programmer.
Maybe it would be more accurate if we called it pass by reference value maybe that way it would be more obvious as to what is going on.
Maybe it would be more accurate if we called it pass by reference value maybe that way it would be more obvious as to what is going on.
Quote:
Original post by 5MinuteGaming
well then maybe it should be a new type of passing parameters to functions since it's not exactly passing by value because you are able to manipulate the object and the whole pointer thing is nonexistant to the programmer.
Maybe it would be more accurate if we called it pass by reference value maybe that way it would be more obvious as to what is going on.
Java References == C++ Pointers.
JavaObject obj = new JavaObject(); //obj is a pointer to a memory address on the heap that contains an instance of the JavaObject classCppObject* obj = new CppObject(); //obj is a pointer to a memory address on the heap that contains an instance of the CppObject classvoid javaMethod(JavaObject param){} //param is passed by valuevoid cppFunction(CppObject* param){} //param is passed by valueJava References == C++ Pointers. Just because the syntax is different doesn't mean "the whole pointer thing is nonexistant to the programmer".
You can even deterministically delete a java object. Make sure the object disposes all resources (disposable pattern), set all references to the object to null, and explicitly invoke a garbage collection.
Java has NO pass-by-reference. It is ONLY pass-by-value. There is no need to come up with a new name, you just need to learn what is going on with the language you are using. It's pass-by-value, and objects variables are pointers. That's all you need to know.
Quote:
Original post by capn_midnight Quote:
Original post by 5MinuteGaming
well then maybe it should be a new type of passing parameters to functions since it's not exactly passing by value because you are able to manipulate the object and the whole pointer thing is nonexistant to the programmer.
Maybe it would be more accurate if we called it pass by reference value maybe that way it would be more obvious as to what is going on.
Java References == C++ Pointers.JavaObject obj = new JavaObject(); //obj is a pointer to a memory address on the heap that contains an instance of the JavaObject classCppObject* obj = new CppObject(); //obj is a pointer to a memory address on the heap that contains an instance of the CppObject classvoid javaMethod(JavaObject param){} //param is passed by valuevoid cppFunction(CppObject* param){} //param is passed by value
Java References == C++ Pointers. Just because the syntax is different doesn't mean "the whole pointer thing is nonexistant to the programmer".
You can even deterministically delete a java object. Make sure the object disposes all resources (disposable pattern), set all references to the object to null, and explicitly invoke a garbage collection.
Java has NO pass-by-reference. It is ONLY pass-by-value. There is no need to come up with a new name, you just need to learn what is going on with the language you are using. It's pass-by-value, and objects variables are pointers. That's all you need to know.
you mean to say Java References are LIKE C++ Pointers remember Java uses bytecodes.
Pass-by-value or pass-by-reference or pass-by-reference-value who cares you can manipulate an objects member data in a function you can't change the this pointer or which object the variable you passed is referencing/pointing to that would be all that mattered if in fact any of it did matter.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement