*
The moose likes Beginning Java and the fly likes Cannot Understand The Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot Understand The Output" Watch "Cannot Understand The Output" New topic
Author

Cannot Understand The Output

R. Joshi
Greenhorn

Joined: Jan 04, 2013
Posts: 22

I just started learning java, while learning I came across a code .

public class Sample {
public static void main(String[] arr) {
System.out.println( '1' + 1 );
}
}

It is printing " 50 " as output. Can anyone please tell me how it is printing 50 . I cannot understand.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Here is the code again with code tags:


See how '1' comes out as blue while 1 comes out as brown(ish)? That is because '1' is a char (character) and 1 is a number. A char is actual a special type of integer (number) where the value is a code point - a number that says 'look this value up in your character table and display the character there'. The actual code point for '1' must be 49. So 49 plus 1 is 50


Steve
R. Joshi
Greenhorn

Joined: Jan 04, 2013
Posts: 22

But when I am running:-

public class Sample {
public static void main(String[] arr) {
System.out.println( '1' );
}
}

It is printing 1 as output..... So if '1' represents 49 in the code point, then it should print 49 as output but it is not.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18702
    
  40

R. Joshi wrote:But when I am running:-



It is printing 1 as output..... So if '1' represents 49 in the code point, then it should print 49 as output but it is not.



Unless, hint hint hint, the method is overloaded. I wonder what will happen if you cast the '1' to an int?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Not quite. Because '1' is a char, and a char will be printed as the corresponding character (I think it uses Character.toString(), if I remember correctly).

But '1' + 1 is a char added to an int. And according to the rules of Java integer arithmetic, that results in an int (any integer type added to any other results in an int, unless one of them is a long, in which case it results in a long). And an int will be printed using Integer.toString().
R. Joshi
Greenhorn

Joined: Jan 04, 2013
Posts: 22

Henry Wong wrote:
Unless, hint hint hint, the method is overloaded. I wonder what will happen if you cast the '1' to an int?


After casting it is printing 49 , I got it , it is treating '1' just as an integer value in print statement..is it???

And which method is overloaded please explain.... Also why can't character be cast to Integer using Integer.parseInt(); what is the difference b/w type casting & type conversion..
R. Joshi
Greenhorn

Joined: Jan 04, 2013
Posts: 22

Matthew Brown wrote: (I think it uses Character.toString() ).
And an int will be printed using Integer.toString().


Does System.out.println() use Character.toString () to print character values & Integer.toString() to print integer values???
which method calls these two functions to print ???
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
R. Joshi wrote:Does System.out.println() use Character.toString () to print character values & Integer.toString() to print integer values???

Possibly, but the actual implementation details are unimportant (and may actually be different in different versions of java).

R. Joshi wrote:which method calls these two functions to print ???

One of the overloaded versions of println.
Look at the javadoc for the PrintStream class (which is what System.out is) and see how many versions of the println method there are.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
R. Joshi wrote: . . . Does System.out.println() use Character.toString () to print character values & Integer.toString() to print integer values???
which method calls these two functions to print ???
Have you looked at the documentation which Joanne mentioned? That actually answers the question.
R. Joshi
Greenhorn

Joined: Jan 04, 2013
Posts: 22

Thanks to you all for the help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot Understand The Output