• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

"conversion" of primitive types

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ppl,
In RHE talking about Unary operators the authors say(their words):
"For unary operators, two rules apply, depending on the type of the single operand:
* If the operand is a byte, a short, or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens).
* else there is no conversion."
Now, if I do this:
<code>
char c='j';
c++;
System.out.println(c);
</code>
it does display char 'k'.
So
1) what exactly does this "there sill be no conversion meanins?"
2) I guess char c was converted to int, applied the ++ operator and again converted to character. Am I right?
Thanks,
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding...
char c='j';
c++;
is equivalent to:
char c='j';
c = (char)(c + 1);
1 is an int so c is promoted, then implicitly cast
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char c was converted to int and added 1. When printing c, you are actually invoking System.out.println(char x) since c's really type is char.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anup Engineer:
* If the operand is a byte, a short, or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens).



Didn't you write this yourself?

And you don't believe your own writing?
There is no conversion necessary here:
char c = 'j';
c++;
c is of type char, but don't forget that char is an integer type (Not int, integer, as opposed to floating point.) It's a 16 bit quantity. Adding one to it is something easily done without the need to convert it. This is one of those special cases you need to remember.
The following, however, causes the result to be converted to an int:
c +=1;
However, it's also implicitly narrowed back to a char for you!
But this will result in a compiler error:
c = c + 1; //must explicitly cast result type back to char
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're RIGHT! Thanks for the good explation!
Tina
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic