   [java] call by reference(speed question)
  comparised to this c code:void::someBlacall(int & what){   what = 511* what;}and the java codeprivate void someCall(int[] what){    what[0] = 511* what;}or the alternativeprivate int someCall(int what){   return what*=511;}what's the fastest?   
  comparised to this c code:void::someBlacall(int & what)//whats void with the scope operator?{what = 511* what;//c code? dont you mean c++? what *= 511;}and the java codeprivate void someCall(int[] what){what[0] = 511* what;//this isnt correct}or the alternativeprivate int someCall(int what){return what*=511;}what's the fastest?why not time it.i dont even think   ;  Please give a real world example where you need a method which manipulates an integer belonging to another class.Don't waste your time with synthetic micro-benchmarks just to compare the "speed of java" to "the speed of c".;  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<int> 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.;Quote:Original post by LorenzoGattiThere 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 referenceint[] 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 LorenzoGattiThere 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, butsomeMethod(int c) would be most surely inlined if possible.   ;Quote:Original post by Fred304Everything 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? Imagine someone that doesn't know how Java works, he reads this and thinks: "OMG!!!!1 The entire contents of the class are copied to a new region in memory!!!!1 Java sucks, that's why it's so slow".I have no idea of why people always tend to choose the "sensationalistic claims" way.;Quote:Original post by Anonymous PosterQuote:Original post by Fred304Everything 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 PosterQuote:Original post by Fred304Everything 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 Fred304The 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 ;)I am going to resume your explanation for the beginners in real world terms, ok?- You can change the contents of a class you passed to a method but not the reference itself. This is not bad, this is a different way of working than some other platforms. People usually want to do this:List list = new List();populateList(list);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.- 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.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.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.   