• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer to Char output question

 
Markus Zuckerberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

This is my first post here!

I'm java beginner and have a simple question:

I initialize an integer calc = (char)100 +(char)12; the integer sum output would be 112, but I'd like to print out the ascii-char ("p") of the output of 112 and not the integer sum of 112.

how can i do that?


thanks in advance,

regards
markus
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting has a higher precedence than addition, so you should explicitly make sure the addition is performed before the cast:
 
Markus Zuckerberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Casting has a higher precedence than addition, so you should explicitly make sure the addition is performed before the cast:


Thanks, but I'd like to convert a char into another char with an addition like:

char a = 'P';
char b = a + (char)32;

print out... char b

I thought the char P is stored as an integer value and i can calculate with this value, add another value and give it back/out as an ascii-char?

Thanks in advance,
markus


 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can do that, but addition automatically promotes its operands to at least an int. So what you need to do is the following:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic