• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final Variable Behavior

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain as to why Program 1 shown below compiles & runs BUT Program 2 doesn't ?



Program 1 :
----------

public class SCJP{

public static void main(String[] args) {
final int x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}





Program 2 :
-----------

public class SCJP{

public static void main(String[] args) {
final int x;
x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you declare a variable final, a change of its content is prohibited by the compiler.
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Sid, The error is related to Casting rather than assignment to final variable.

Moreover I am not changing the content of a final variable in any of the above program
[ July 24, 2008: Message edited by: Saurabh Vyas ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program 1 :
----------

public class SCJP{

public static void main(String[] args) {
final int x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}



This is a case of implicit narrowing conversion...Generally, narrowing conversion requires explicit casting. But narrowing conversion can also be done implicitly provided the following 3 conditions are satisfied :
1. The source (in this case, it is x) is a constant expression of either byte, short, char, or int type

2. The destination type (in this case, it is y) is either byte, short, or char type

3. The value of the source is determined to be in the range of the destination type at compile time

Coming back to program 1 : First look at the statement
Here you are assigning a integer value to a short variable. So, this is narrowing conversion. Now, you need to find out whether you need an explicit casting or not.. here, final variable x is assigned a value of 10 during the declaration stage itself..so, x becomes a compile time constant & thus satisfying condition # 3. As you can see, conditions 1 & 2 are already satisfied.. thus it becomes implicit narrowing conversion..

Program 2 :
-----------


public class SCJP{

public static void main(String[] args) {
final int x;
x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}



In this case, you are NOT assigning the value of 10 to the final variable x during the declaration stage itself.. so, now, the value of x can be changed elsewhere in the program..in other words, you can say that the compiler will treat x as just another normal variable.. so, x is not a COMPILE TIME CONSTANT anymore and thus not satisfying condition # 3..
Because of this reason, you need to explicitly cast the narrowing conversion in order to make program # 2 work..
i.e. short y = (short) x ;

Hope this can solve your doubt..
Do let me know if you have any queries, I will try to explain it in a better way..

regards,
naveen
" It's the attitude and not the aptitude that determines the altitude"
 
Naveen Kumar G
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi saurabh,
I will give you some more examples where the compiler will show error.
 
Sid Murrey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Saurabh Vyas:
Sorry Sid, The error is related to Casting rather than assignment to final variable.

Moreover I am not changing the content of a final variable in any of the above program

[ July 24, 2008: Message edited by: Saurabh Vyas ]



Oh yeah, I'm sorry. I was not looking very carefully!
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naveen

That was really informative.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naveen..Now its clear to me.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ..
That was agood explanation..
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are to kinds of final variables: Compile time constants and the others. In the first example a compile time constant is used, in the second example not. I explained the difference between the two in detail in my contribution in the following topic.
 
Naveen Kumar G
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all ...

all the best for your SCJP

A detailed explanation on type conversion & numeric promotions are given in the book written by Khalid Mughal.. I am not sure whether it is in K&B..

regards,
naveen

"It's the attitude and not the aptitude that determines the altitude!"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic