• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Automatic conversion of the byte, short and char

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic