• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can some one explain this for me?  RSS feed

 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
it means we are changing the value from 1 to 0?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1837
10
  • Mark post as helpful
  • send pies
  • 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.
 
Karthick Pattabiraman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you read

http://www.javaranch.com/campfire/StoryPassBy.jsp
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!