File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can it be valid? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can it be valid?" Watch "How can it be valid?" New topic
Author

How can it be valid?

ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
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

Joined: Nov 12, 2000
Posts: 183
i think beacause they are in the range of Byte.
quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
Don't you need explicit cast to make them valid?
rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
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

Joined: Mar 10, 2001
Posts: 151
the statement
byte c=(char)16.2;
gives compile error needing explicit casting from char to byte.
Jo Oehrlein
Greenhorn

Joined: Jan 03, 2001
Posts: 11
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

Joined: Nov 30, 2000
Posts: 111
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

Joined: Mar 10, 2001
Posts: 151
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

Joined: Mar 30, 2001
Posts: 2
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can it be valid?