Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain the reason behind the output of this code

 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output is:

macs-macbook:desktop mac$ javac Test.java
macs-macbook:desktop mac$ java Test
?
?
?
?

(after 4 ?'s there is a space shown for division operation by JVM)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
characters are really integer values. You can add them together. If you look at an ascii chart, you'll see that 'a' is equal to 97, and 'b' is 98. so when you add ch + 'b', you effectively have 97 + 98, or 195.

When you then try and print this, it is outside the normal printable range, so you can get something weird.
 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same Code mentioned above, if i run on windows platform it prints distinct special characters may i know the reason behind and with a detail explaination
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure as I have never worked on a Mac, but maybe it tries to print using the extended apple table instead of unicode.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The console (cmd.exe on Windows, bash / csh / ... on Linux) is very limited in the characters it can display. Use JOptionPane to show the chars will be more informative, although it still can't display all characters:
 
Campbell Ritchie
Sheriff
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to follow Fred's suggestion of getting an ASCII chart, or a basic Unicode chart, which has the same values. Add the two chars together (remembering Unicode gives their values in hexadecimal, so use the hex option on your computer's calculator), and compare them with the ASCII values or the "extended" Unicode chart. Then you can see what the sum is supposed to mean. If you go beyond those two charts, the Unicode site will give lots more characters.
Remember that the DOS and Windows® command lines only have a restricted range of characters they can display, so even something like £ comes out as ú on it. And many characters simply come out as boxes or ?
 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you your reply's helped me for the better understanding of my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic