• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Initialization!!!!!!!!

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know what this quote means
"The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char."
its Foreign Language to me
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know the compiler allows this

Without that, you wouldn't be able to assign an integer constant (24) value to a type smaller than int without casting. Since the value of 24 is known at compile time, the compiler can check it for overflow (try using 240 instead of 24, the compiler will complain).
Since a final variable is, well, final , the statement above is saying this will work also (and it does).

HTH,
Chris
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Chris, This definitely helps.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing to be aware of. This won't be the case when it comes to method's invocation.
//Byte(byte b);
Byte b = new Byte(2); won't compile though 2 is in range
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...now I am confused again
Byte b = new Byte(2);
will not compile?? whats wrong with that
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I tried to compile above line and it did show error, but then I tried
byte x = 2;
Byte b = new Byte(x);
and this work fine. Does anyone know why? and does this happen just to Byte or just Byte and Short?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer was given by Shishio.
The narrowing implicit conversions are not allowed in the parameters to methods.
new Byte(byte); will only accept a byte compile type
This is ok "new Byte((byte)2);"
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic