aspose file tools
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
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 514
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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Automatic conversion of the byte, short and char
 
Similar Threads
Unary operators
++ / -- convert to int RHE chap 4
Arithmetic promotion
byte.......
Primitive conversions