File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Unary operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unary operators" Watch "Unary operators" New topic

Unary operators

dhruv simaria

Joined: Mar 26, 2001
Posts: 6
well Simon Roberts in his book has mentioned, that for Unary operators ,2 rules aply. One of them being that if the operand is byte,short or a char,it is converted to an int.
Using this rule
When you run this code ,the output should be type mismatch, as we can't convert int value to char type without casting,but surprisingly when you run this code you get the answer as - The value is b

[This message has been edited by dhruv simaria (edited March 26, 2001).]
[This message has been edited by dhruv simaria (edited March 27, 2001).]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Hi, welcome to JavaRanch. This particular forum is intended for discussion about JavaRanch itself, not for questions about the Java language. I am moving this post to Java in General (Beginner), which is a good place for your questions about the language.

"I'm not back." - Bill Harding, Twister
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
In your example, when using the "++" operator there is numeric promotion of type char to type int, so the char value is converted to its numeric equivalent and then it is increased. There is also an implicit cast back to type char and it casts the increased numeric value to char which why the char value cahnged.
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Yes it is perfectly acceptable to use a unary operator on a char type.
dhruv simaria

Joined: Mar 26, 2001
Posts: 6
Thanks Jim for welcoming me.Sorry about that goofup that being ,me posting my question on the javaranch instead of posting it here.
P.S : Thanks Herb for clearing my doubt
I agree. Here's the link:
subject: Unary operators
jQuery in Action, 3rd edition