File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to print Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to print "0"?" Watch "How to print "0"?" New topic

How to print "0"?


Joined: Mar 02, 2004
Posts: 1
I'm a beginner in Java.I have found this code on BBS,the man said:this code's output is"48",if I key in"0".Why? Is it because that "int num" is transformed to "String num"???
Then,how can I print "0"?

public class first
public static void main(String[] args)
int num= 0;
num =;
catch(Exception e){}
System.out.println("the input is:"+num);
[ March 23, 2004: Message edited by: tsai zhong-ling ]
[ March 23, 2004: Message edited by: tsai zhong-ling ]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

when someone types in 0 at the prompt, they are typing in the CHARACTER '0', not the integer value of 0. since you are working in an ascii world, '0' is encoded as 48, stored in your int, then printed out.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281

The nice thing about Standards is that there are so many to choose from!
I agree. Here's the link:
subject: How to print "0"?
It's not a secret anymore!