This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why final modifier has such impact

 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code produces compilation error but if "i" is decalred as "final",it doesn't!. Can anyone explain the impact of final on widening of primitive types.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not widening, you are narrowing. You would normally require a cast for the b = i; assignment. I can only presume that the compiler can tell that final int i = 2; implies the value of i will always be within the range of a byte. Try changing i to 128, and see whether it still works; 128 is outwith the possible range for bytes.
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, I got how it works behind.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is right: If you make i final, then the value of i becomes a compile-time constant: a value which is fixed and known at compile-time - so the compiler can check if it fits into a byte.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic