• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final arrays and initialisation

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have the following:

final int[];

will all the cells in the array have their value set at 0(int's default value)
or is the final modifier just to prevent reassigning the reference??
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duran Harris wrote:If I have the following:

final int[];

will all the cells in the array have their value set at 0(int's default value)
or is the final modifier just to prevent reassigning the reference??


assigning values is a runtime thing
and
reassigning check for final's is compile time thing

here you are declaring an array reference that will point to group of int's
the values of these int's can be changed but the array reference will continue to point to this group of int's till its lifetime.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give you an example of what Sandeep just said

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok good..

then array+final behaves exactly the same as:
object+final

Thank you
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with final you can not do changes. other example with variables see:


This code show error, when you try to compile. because I can not change one value of one variable final, it is impossible.

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would probably laugh at this but when I started programming I tried to do this :lol:

int i = 5++;
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case there is still hope for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic