• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

FINAL VARIABLE

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cosidering the following -

byte a = 10 ;
final int x = 10 ;

Which of the following assigments are valid ?

a)byte y = x ;
b)char ch = a ;
c)char c = x ;
d)short j = a = x ;

here the answer is a,c,d

when the x is declared as final then how can you assign the value of x to y ,c,j
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karthik Rajashekaran:
...when the x is declared as final then how can you assign the value of x to y, c, j


You can't assign a new value to x because it's final. But y, c, and j are not final, so you can assign them any valid value.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is a little more to it than that. What about

Although, 10 is within the range of an int, this should throw a compiler error.

The difference between this and the example given by Karthik is that byte has some special rules. That is an implicit narrowing conversion takes place when assigning an int to a byte if the value is within the range that is allowed for a byte. I guess a similar rule applies for char, although I am unfamiliar with it.

Layne
 
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If implicit casting occurrs for answers a.),c.),d.), then answer b.) also should be correct .

rgrds,
Shankar
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, 'char ch = a' fails at compile time with 1.4 and the compile complains that there is a possible loss of precision.


Here lines 1,2,3 all fail at compile time as well, yet lines 4 and 5 are ok. Since characters are unsigned 16 bit primatives, it appears that initializing them with numbers that fits in the range is ok but when when referencing a char to any other primative a cast is needed. Perhaps this is because all other primatives are signed (except boolean). Could those out there please enlighten us (me). Thanks!

cheers, Andreas
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the reason for this strange behaviour as pointed?I mean downcasting is not allowed in Java without explicit casting.But how come its working here?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A literal int value or a final int variable can be assigned to a narrower primitive type (byte, short, or char) without explicit cast if it falls within range. However, a non-final int variable requires an explict cast or a compiler error will result.

Despite being "narrower," a non-final byte or short is not convertible to a char without an explicit cast. (Consider that a byte or short could be negative, but a char cannot.)

[ September 21, 2005: Message edited by: marc weber ]
 
Andreas Sandberg
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explaining that so clearly. I'm curious as to what the thought is behind not forcing the cast for 'final int'. From testing this out a bit it does appear however that:


What's the thought process behind this? Thanks!

Andreas
[ September 21, 2005: Message edited by: Andreas Sandberg ]
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow great explaination

thanks a lot
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found some strang thing ...

If i remove final keyword in the original code posted by Karthik as:

byte a = 10 ;
int x = 10 ;

byte y = x ;//1
char ch = a ;//2
char c = x ;//3
short j = a=x ;//4

then compiler giver error as:
possible loss of precision at 1,2,3,4.

so why is this different behaviour with and without final keyword?



also

an implicit narrowing conversion takes place when assigning an int to a byte if the value is within the range that is allowed for a byte.



In this case also the behaviour is different with and without final keyword

Can anyone explain this???
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's actually a little more complicated than I explained above, because I left out an important detail: In order to be implicitly cast to a narrower type, the value of the final variable must be known at compile time. It can then be treated essentially as a literal.

In the code below, there are 2 final int variables. The value of one is known at compile time, but the other value is not known until runtime. The one with a compile-time value can be assigned without explicit cast to a byte (if within range), but the one that does not have a known compile-time value cannot.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for that explaination about the "final" variable and "narrowing" process. Even I had got stuck with this and was not able to understand the thought process of the compiler.
reply
    Bookmark Topic Watch Topic
  • New Topic