• 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

Can some one explain this for me?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This was on my exam and I know the answer is 4. Can someone tell me what does do in here? Why the is it 4?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's substitute the method parameters into the containing code

What do you think that bit of code does?
 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it means we are changing the value from 1 to 0?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to answer your question with a bunch of questions which will hopefully lead you down the path to enlightenment, or at least give you some fruit for further research.


It is 4 because the value '4' got assigned into the array at index 0.
Why would it not be 4?
What other value do you think it might have been? Why?

This question is testing the rules around parameters passed to methods.
Java is strictly pass by value.
This means that any changes to the value of a parameter in a method, it is not reflected in the calling code.

Does editing the contents of an array qualify as changing the value of "array"?
What sort of operation WOULD change the value of "array"?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite. It means that the element at index 0 of the array is assigned the value 4.
 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks, I got that part, but I still don't get what does do here?
if we keep changing this numbers to or for example, the answer is still 4. so I just don't understand this part.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amir Ghannad wrote:Ok thanks, I got that part, but I still don't get what does do here?
if we keep changing this numbers to or for example, the answer is still 4. so I just don't understand this part.



Yeah, you are going to need to show us the modified code, because with the code you shown so far, making the changes you mentioned does not produce 4.

Henry
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GOT IT! I was my own mistake! you were right 4 was not the right answer for the modified code.

Thanks for your help.
 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question, I know that the output for this code is 1. Is it because we are having this in var=1? what does do here?
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Adding a few more print statements to the code will highlight what is actually happening in this code.

It is the same concept: Changes to the value of any parameters to a method do not affect the original variable it was called with.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you read

http://www.javaranch.com/campfire/StoryPassBy.jsp
 
reply
    Bookmark Topic Watch Topic
  • New Topic