This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Integer to Char output question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer to Char output question" Watch "Integer to Char output question" New topic
Author

Integer to Char output question

Markus Zuckerberg
Greenhorn

Joined: Sep 25, 2011
Posts: 2
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

Joined: Sep 20, 2010
Posts: 3573
    
  14

Casting has a higher precedence than addition, so you should explicitly make sure the addition is performed before the cast:
Markus Zuckerberg
Greenhorn

Joined: Sep 25, 2011
Posts: 2
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

Joined: Sep 20, 2010
Posts: 3573
    
  14

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:
 
Don't get me started about those stupid light bulbs.
 
subject: Integer to Char output question
 
Similar Threads
Need help to output unsigned byte > 127
Declarations and access control
doubt
Using toString() Method
Array Project Help