• Post Reply Bookmark Topic Watch Topic
  • New Topic

final  RSS feed

 
umang bhartia
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every1
code:
case 1:
final int j =10;
byte b =j;// no error

case 2:
int i =10;
final int j = i;
byte b = j;//error
In first case it compiles fine, and b is assigned the value of j. But in second case it gives me error. Can some one throw some light as to why this is giving error in one case but not in another.
thank u
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
It seems to be a java bug.It should give error.Because we are narrowing interger from byte.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean. in case 1 if you take away keyword final, then it won't compile as expected because you're attempting to put a int into a byte. I checked Khalid's book and under the heading final members I found this interesting quote:
"For final members, the compiler is able to perform certain code optimizations because certain assumptions can be made about such members." -p123.
I think in case2 when you introduce another variable and then assign it into the final member j, the compiler is no longer allowed to make "certain assumptions" because then we see the compiler error of trying to put an int into a byte. That's the best I can offer you. I hope someone with a better idea comes along and explains this to us.
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried another one
class wrap
{
public static void main(String args[])
{
final int j =127;
byte b = j;
System.out.println(b);
}
}
It compiled .Then I tried this
class wrap
{
public static void main(String args[])
{
final int j =128;
byte b = j;
System.out.println(b);
}
}
This gives error
F:\javasid>javac wrap.java
wrap.java:6: Incompatible type for declaration. Explicit cast needed to convert
int to byte.
byte b = j;
^
1 error

Explanation:
When you chose the varibale to be final.Java stores it in smallest possible container.So,When you assign integer a value in the range of byte.Internally it stores only on single byte.
As soon as you go for a number that can't be stored it gives error.
It was a nice experiment.So your question gave opportunity to explore undocumented logic of final!!!
Congrats
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is pretty interesting. I wonder if that is the type of question that would end up on the exam?
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess such questions won't be in exams.Beacuse they don't provide compiler at Test Centre :-)
 
avuthu shad
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When the data is final the data cannot be changed ,Because of that reason final does optimization on the data type, if it is in the range of byte then casting is not required.(-128 to 127).
But this optimization by complier for final works only for int and short not long.
check it out.
But the main reason is still not known.
------------------
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i saw a post saying he or she got a question like that.
Know the Fundamaentals guys!!
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is explained in JLS 5.2
I guess is important enough to learn for the exam.
Constant expressions are defined in JLS 15.28
This is from there:
" Simple names that refer to final variables whose initializers are constant expressions
"
so "final int j = i;"is not a constant expression and considering JLS 5.2 the compiler complains when "byte b = j;//error"
hope helps
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!