• 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

byte b =(int)16.2; Is it possible? How?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

Is the below line is possible, My understanding so far it requires explict casting. Higher bit to lower bit(narrow), Explict casting is must.

BUT it compiles fine and RUNS fine and I m confused and lost my understanding.

The output is 16.

Help please to bring back my confidence on the above fundamentals.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ponraj

As per my understading the line "byte b = 16" works becuase the value is in byte range that is -128 to 127.
If you try to assign the value out of range it will throw compile time error.

If you try to compile the line "byte b = 16.2", you will get compile time error:
possible loss of precision
found : double
required: byte
byte b =16.2;
^
1 error

so if you compile line "byte b = 16.2" with the explicting cast as "byte b = (int)16.2", you get output as 16 which is the integer of part of number 16.2.

Regards,
Varsha
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What we need to understand is that the compiler has "some smarts" but not a very good memory. By this, I mean that the compiler compiles one line at-a-time.

byte b1 = (int)16; // this works because the compiler can tell by this line that 16 is in-range

whereas

int i = 16;
byte b2 = (int)i; // this doesn't work because the compiler can't tell by this line alone whether the assignment is in-range or not.
 
T Ponraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Kaydell, I understood the point, now.


Same thing like what you sid here,

char c = 16; //works fine

char a = 16;

char c =a; doesnot work.
 
T Ponraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then why byte b =(long) 16.2; is not working, If we apply the same concept.

Many Thanks for your time..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic