This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can it be valid?

 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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?
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think beacause they are in the range of Byte.
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you need explicit cast to make them valid?
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
the statement
byte c=(char)16.2;
gives compile error needing explicit casting from char to byte.
 
Jo Oehrlein
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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?
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
scott nichols
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic