• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

casting rule doesn't apply?

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the code
class i{
final int i=10;
byte k=i;
}

it compiles even though an int can't be casted to a byte implicitly.
but when I remove 'final' from the first line it doesn't compile.
I also tried
final long i=10;
byte k=i;
it dosen't compile.
Why the hell it compiles with a final int??
please someone help me.
Nasir
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java will do implicit narrowing in assignment, if the value of the variable can be determined at compile time. So this would include integer literals whose values is within the byte data type range. And this also includes final variable which are constants hence there values can be determined at compile time.
zulfiqar raza
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks zulfiqar
How about
final long i=10;
byte k=i;
Why doesn't it compile ?
Here i is also a final varible and so is a constant.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
(Used: Java Language Specification 2nd ed.)
[skip to "RECAP" at bottom if too long or confusing or ...]
[5.2 Assignment Conversion]
"...a narrowing primitive conversion may be used if all of the following conditions are satisfied:
(1)-The expression is a constant expression of type byte, short, char or int.
(2)-The type of the variable is byte, short, or char.
(3)-The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable." [I added (1)- (2)- and (3)- to quote]
final int i=10;
byte k=i; //all 3 cond. met thus no compile-time error
// "variable" is 'k'. "expression" is 'i'
// (2) met: 'byte' is the type of the variable k.
// (3) met: '10' is value of expression and it fits in a 'byte'
// (1) met: 'i' is a constant expression of type int (not long).
// To explain "constant expression" consider:
[15.28 Constant Expression]
"A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following: ... Simple names that refer to final variables whose initializers are constant expressions" [I skipped the irrelevent criteria using "..."]
In your example, 'byte k=i', "i" is the expression, a "constant expression" because "i" is a simple name that refers to a final variable ("i" a.k.a. "[somepackagename].i.i") with an initializer that is a constant expression ("10" in 'final int i=10;').
Here's an example:
a=57; // "57" IS a constant expression.
b=a; // "a" WOULD be a constant expression IF it was
// a "blank final" that was initialized with 'a=57;'
// in which case "a" would be a simple name refering to
// a final variable (whose initializer is a constant
// expression, "57"). -- Somewhere earlier would be the
// line 'protected final a;' (or something similar).
// If INSTEAD 'protected a;' appeared (no "final") then a
// compiler would (should) report an error. that is,
// "a" WOULD then NOT be a constant expression.
So,
protected final a; // or "final a;" or "static final a;" etc.
a=57;
b=a; // a is const. exp.
But,
protected a; // here, not a blank final as above.
a=57;
b=a; // a is NOT const. exp.

***************************************************************
RECAP:
class i{
final int i=10;
byte k=i; // legal ...
}
-k of type BYTE (char, byte, or short)
-k initialized by CONSTANT EXPRESSION of type INT
(char, byte, short, or int -----not long!)
-the constant expression fits in k (i==compile time const. 10)
-Finally note, "i" is const. exp. via [15.28]
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jose
I should've gone through JLS
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a question .....Beautiful.....
prasad
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a beautiful explanation on maha's page but anyway I will try and clear your doubt when you assign final to a variable then the value cannot be changed so int i gets the value 10 and 10 can be easily cast into byte without explicit conversion so it doesnot give any compilation error but long what so ever may haapen caanot be cast into a byte.Hope this clears your doubt.
regards,
Shankar.
 
reply
    Bookmark Topic Watch Topic
  • New Topic