• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parameter Use

 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying for the SCJP. Please take the following code snippet.



Now I have no problem with lines I have commented, as it makes sense to me. What does not make sense to me is why d[0]++ or c[0]++ works.

c[0]++ --> seems to me this changes the value of final int[] c

since arrays are passed as reference d[0]++ seems like it changes the value of final int[] d = {3}; in the main method.

Is it only the memory address pointing to the array object that cannot be changed or reassigned? For some reason I figured that the values within the array were also unchangeable.

Can somebody please clarify for me?

Thanks,
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Gorder wrote:
Is it only the memory address pointing to the array object that cannot be changed or reassigned? For some reason I figured that the values within the array were also unchangeable.


No. When declaring a final variable, only the variable is not changeable. And in your example, the variable is a array reference. The reference can't be changed, but the array can be changed.

Henry
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks for clearing that up for me Henry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic