• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Casting

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one please help me with premitive casting.
How to use figure shown below in the practical way.
Byte –> short -> int -> Long-> float-> double
Byte b = 1; //I think this means int to byte
which is not traceable in the figure above.
So it shouldn't compile.
I know this does compile, I am confused?
Thanks
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works as long as what you are trying to assign to a byte fits within a byte's range, in this case 1 is perfectly admissible for a byte. Actually, you can do this fo all integer literals up to 127 and -128.
Please check out JLS 5.2 Assignment Conversion for more information.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The compiler allows you to assign values to variables without casting, providing that the assigned value is known at the compile time and the value is in the range of the variable type.
Examples:
The code:
int i = 1;
byte b = i;
will not compile. Explicit cast needed.
The code:
byte b = 125;
will compile. Assigned value is known at compile time.
The code
b = 200;
should not compile. Range for byte is -128..127.
Hope that helps
 
Sachit Mehta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
It make sense now.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code:
int i = 1;
byte b = i;
will not compile. Explicit cast needed.

Just to add something more, the following code compiles fine because the compiler has a way to know that i has the value 1 at compile-time:
final int i = 1;
byte b = i;
Remember that a compiler does not intrepret the Java code it parses, and thus, has no way to know the value of a variable during the parsing process. It can only know the value of constants (final fields) since they are definitely fixed at compile time.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The one area that gets a little sticky is float.
float f = 0.0;
will not compile (possible loss of precision).
You can of course specifiy the literal is a float and it will work.
float f = 0.0f;
I know part of the problem with 0.0 is that real literals are doubles. Past that I think the compiler is afraid of dropping significant digits.
I'd like to hear other folks take on why the float primitive is different.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because of the potential loss of precision:
float f = 2.0/3.0;
would (if it would compile) give a different result than:
double d = 2.0/3.0;
but
byte b = 2/3;
short s = 2/3;
int i = 2/3;
long l = 2/3;
all give the exact same result.
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sachit Mehta:

Byte –> short -> int -> Long-> float-> double
Byte b = 1; //I think this means int to byte


I want to just add to something here....
Sachit,
I guess its a typo and you already know this
just in case
a byte is different from Byte and similarly
a long from a Long
Sri
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic