• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unary operators

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Why?

[This message has been edited by dhruv simaria (edited March 26, 2001).]
[This message has been edited by dhruv simaria (edited March 27, 2001).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is perfectly acceptable to use a unary operator on a char type.
 
dhruv simaria
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic