wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes please explain the reason behind the output of this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please explain the reason behind the output of this code" Watch "please explain the reason behind the output of this code" New topic
Author

please explain the reason behind the output of this code

Tarun wadhwa
Greenhorn

Joined: Jan 29, 2011
Posts: 14


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)


"The More you Play With it, The more you will learn ". "Keep Playing"
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tarun wadhwa
Greenhorn

Joined: Jan 29, 2011
Posts: 14

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

Joined: Mar 20, 2007
Posts: 317
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

Joined: Oct 27, 2005
Posts: 19695
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Jan 29, 2011
Posts: 14

Thanks to all of you your reply's helped me for the better understanding of my problem.
 
 
subject: please explain the reason behind the output of this code