my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why the output of x is so strange? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the output of x is so strange?" Watch "why the output of x is so strange?" New topic
Author

why the output of x is so strange?

jimmy
Greenhorn

Joined: Nov 06, 2004
Posts: 18


( tags added and corrections made to variable names)
[ November 28, 2004: Message edited by: Barry Gaunt ]
jimmy
Greenhorn

Joined: Nov 06, 2004
Posts: 18
sorry,
should be :
System.out.println(" x= " + x + " prefix value=" + resultPre);
System.out.println(" x= " + x + " postfix value=" + resultPost);

and the outprint is :
x=--diamand sign-- prefix value = 5
x=--diamand sign-- postfix value = 4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
That's because variable x is declared as a char. So it tries to print out a representation of whatever unicode character has the value 4 (\u0004).

To get it to print "4" you can cast it to an int as in:



Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
jimmy
Greenhorn

Joined: Nov 06, 2004
Posts: 18
oh, I see, thanks alot
 
GeeCON Prague 2014
 
subject: why the output of x is so strange?