It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Automatic conversion of the byte, short and char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Automatic conversion of the byte, short and char" Watch "Automatic conversion of the byte, short and char" New topic
Author

Automatic conversion of the byte, short and char

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
My Java Cert book states this...
For unary operators, if the operand is a byte, short or char, it is converted to an in (unless the op is ++ or --)
The unary operators are
+, -, ++, --, ~
So given this look at the following code
byte a = 100;
byte b = +a;
This will flag an error due to the casting that needs to take place. It should look like this.
byte a = 100;
byte b = (byte)+a;
This this correct??
-Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
[This message has been edited by Dale DeMott (edited July 12, 2001).]


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Yes, it is correct!
As you said yourself, you have to explicitly cast it because Java promotes a literal to int when you use + or - before it. It is similar to the binary +, -, *, / etc...
I suppose you've tried it out already...
Hidy hooo!
/Kaspar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Automatic conversion of the byte, short and char
 
Similar Threads
++ / -- convert to int RHE chap 4
Unary operators
Arithmetic promotion
byte.......
Primitive conversions