GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes return statement question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return statement question" Watch "return statement question" New topic
Author

return statement question

David Starr
Ranch Hand

Joined: Apr 08, 2013
Posts: 54
The output is 35. I have played around with this code, but why does it not matter that there is a "+ 10"? Why does it not return 30 to be brought down to the main method where 15 would get added to it, and output be 45?

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your method does return value of 30 in this case, but it's not used nor it is assigned to anything.

What your code does is the following:
  • Initially the first array element has the value of 10
  • Inside method you increase that value by 10, hence the first array element has the value of 20 now
  • Your method just returns the value 30, but you don't modify any value of the array here, so i[0] is still 20
  • You print out the value of the first array element increased by 15, hence you get 35 as the output


  • The quieter you are, the more you are able to hear.
    David Starr
    Ranch Hand

    Joined: Apr 08, 2013
    Posts: 54
    Your third point is one of the conclusions that I came to. It makes sense to a point. I was testing it by altering the variable i from an array to a simple int. The result I was getting was 25. Thank you for the reply. I will also dig into more documentation.
    Xiaolong Bao
    Greenhorn

    Joined: Jun 17, 2013
    Posts: 16

    Here is the point,

    In Java, there are differences between passing the values of variables of primitive data type and passing arrays to a method.

    *For an argument of a primitive type, the argument's value is passed to the method.

    *For an argument of an array type, the value of the argument is a reference to an array; this reference value is passed to the method.
    It means the array in the method is the same as the array being passed.


    Thus, the method 'modify' returns a value of 30, but, i[0] became to 20 under the statement i[0] += 10; and according to the preceding
    rule, the i[0] in the 'main' method became to 20. That's why the output is 35 not 45.

    Try to figure out the output of the following code to gain the understanding:





    Moving on is a simple thing, what it leaves behind is hard. You know the sleeping feel no more pain, and the living are scarred. ---A Tout Le Monde by Megadeth
    David Starr
    Ranch Hand

    Joined: Apr 08, 2013
    Posts: 54
    Initially, I thought x would have been 1001 due to x in the statement int x =1 seeming to override x in the statement m(x,y). Turns out x is 1, which leads me to conclude the following (I hope it is correct). Objects traverse method bounds, but primitives do not. The array item value carries over while the primitive does not; and this is why the result is x = 1.
    Steve Fahlbusch
    Bartender

    Joined: Sep 18, 2000
    Posts: 562
        
        7

    ok, you need to some research on references, primitive values and how these values are set.
    Xiaolong Bao
    Greenhorn

    Joined: Jun 17, 2013
    Posts: 16

    David Starr wrote:Initially, I thought x would have been 1001 due to x in the statement int x =1 seeming to override x in the statement m(x,y). Turns out x is 1, which leads me to conclude the following (I hope it is correct). Objects traverse method bounds, but primitives do not. The array item value carries over while the primitive does not; and this is why the result is x = 1.


    The primitive variable's value is passed to the method, in other word, the method received only a value of x.

    After the execution of the method 'm', the variable 'number' holds a value of 1001, but it does not affect the variable x at all, for the variable 'x' itself still remains the value being passed, that is 1.
    Ivan Jozsef Balazs
    Rancher

    Joined: May 22, 2012
    Posts: 867
        
        5

    In Java, there are differences between passing the values of variables of primitive data type and passing arrays to a method.

    *For an argument of a primitive type, the argument's value is passed to the method.

    *For an argument of an array type, the value of the argument is a reference to an array; this reference value is passed to the method.


    It is false. In Java, copies are passed as arguments: a copy of a primitive or that of an object reference respectively.

     
    GeeCON Prague 2014
     
    subject: return statement question