• 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

How can it be valid?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte c=(int)16.2;
byte c=(short)16.2;
byte c=(char)16.2;
all of them are valid, why?
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think beacause they are in the range of Byte.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you need explicit cast to make them valid?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When u typecast 16.2(a floating point literal)to int and since the value comes in the range of int, there is an implicit casting from int to byte.
i hope i am correct.
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the statement
byte c=(char)16.2;
gives compile error needing explicit casting from char to byte.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get compile errors on two of the statements:
byte c = short(16.2)
Incompatible type for declaration. Explicit cast needed to convert short to byte.
byte c = char(16.2)
Incompatible type for declaration. Explicit cast needed to convert char to byte.
On the other hand, these definitions compile without problem:
byte a=(int)16.2;
short b = (short)16.2;
char c=(char)16.2;
Was the question about the explicit cast of 16.2 to an int, a short, and a char? Or was it about the implicit cast of short to byte and char to byte?
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
none of the original three statements is giving a compiler error .if u print them all gives 16.i hope what rajshree said regarding
byte c=(int)16.2;
applies to others as well.
please correct
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i too am getting compile error for 2nd and 3rd statements saying explicit casting needed as Jo Oehrlein has written.i think if an integer literal is within the range of a data type(byte,short or int)implicit casting takes place.however,if a short or char is to be converted into byte explicit casting is neccessary.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran the three statements and they compile and run fine.
According to R & H, the reason that they compile is that the right hand value is implicitly downcasted to the appropiate type before assignment. R & H says this only works for integral types (char, byte, short, int) and that it only works if it is just an assignment not an expression. (page 65, 2nd ed.).
For example:
<pre>
byte x = 2; // Legal implicitly casts 2 to byte
byte x = (char)2; // same thing

x += (char)5; // Legal x now equals 7

x = x + (char)5; // Illegal, the right hand
// operands are promoted to
// int before being added
// and assigned. Requires
// explicit cast to byte
</pre>
Sorry if this was confusing, but check out page 65 in the R & H book for details. (prob different page in an older addition, it is the section on assignment Operators in he Operators and Assignments chapter).

[This message has been edited by scott nichols (edited March 31, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic