Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot Understand The Output

 
R. Joshi
Greenhorn
Posts: 22
Java MySQL Database Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
R. Joshi
Greenhorn
Posts: 22
Java MySQL Database Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Java MySQL Database Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Java MySQL Database Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Java MySQL Database Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all for the help...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic