• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return statement question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  •  
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Greenhorn
    Posts: 16
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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:




     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Bartender
    Posts: 612
    7
    Mac OS X Python
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok, you need to some research on references, primitive values and how these values are set.
     
    Xiaolong Bao
    Greenhorn
    Posts: 16
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Rancher
    Posts: 1044
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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.

     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic