Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why '\n' is not working?

 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You know what, the OUTPUT is


Hi!
Gays
110.......



Why in the line 1,'\n' is working ,but in the line 2 is not?

Best Regards.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice one. A clue is the fact that the code says "100", while the output says "110".
 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O,thanks .
I think I got it,the char cast to the int.
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first version , '\n' is treated as escape character .
In second version , '\n' is promoted to int in order to add to 100 .

That is why result is 100 .
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is operating differentely as expected.

Integer i = 10;
System.out.println(i + '\n' + "hello"); //line1
System.out.println(i.toString()+'\n'+ "hello");//line2

i got the output as

20hello
10
hello


i expected it as
10
hello
-------
10
hello

why the toString() method is integer is not called in the line1?
I read that, in System.out.println() method when sending Object() , then toString() method will be called. In this case not?

rami
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i = 10;
System.out.println(i + '\n' + "hello"); //line1
System.out.println(i.toString()+'\n'+ "hello");//line2

i got the output as
20hello
10
hello

i expected it as
10
hello
-------
10
hello

Note that '\n' is a char, not a string. So it can be added to a numerical type. If you change the single quotes to double quotes you should see what you originally expected (because strings can not be added to numerical types).
[ June 09, 2008: Message edited by: Ulf Dittmer ]
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ram Reddy:
Code is operating differentely as expected.

Integer i = 10;
System.out.println(i + '\n' + "hello"); //line1
System.out.println(i.toString()+'\n'+ "hello");//line2

i got the output as

20hello
10
hello


i expected it as
10
hello
-------
10
hello

why the toString() method is integer is not called in the line1?
I read that, in System.out.println() method when sending Object() , then toString() method will be called. In this case not?

rami



Same problem as above, the character '\n' is treated as the integer it is in that context.

PS Jack, noone can accuse your code of being homophobic, but for the ultimate in political correctness perhaps it should say hi to heterosexuals too.
 
sachin naphade
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
while performing any of the operation using binary operator both side of the operator are converted to integer type if they are char,byte,short before applying the operator since they are compatibles.hence in the second sysout 100 + '/n' are converted to integer so the addition is 110 and then 110+"........" but since integer and strings are not compatible so the out put is 110........ but in case of first sysout this is not the case hence the different result
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply.
I understood Here.
public class Test {public static void main(String[] args){System.out.println("Hi!" + '\n' + "Gays"); // line 1System.out.println(100 + '\n' + ".......");// ling 2}}

But

Integer i = 10; // Integer Object
System.out.println(i + '\n' + "hello"); //line1
System.out.println(i.toString()+'\n'+ "hello");//line2


I am sending the Integer object in line1. So while evaluating the expression JVM won't call toString() method for 'i' in line1.
When i called explicitly , i got the expected output.

rami
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ram Reddy:

But


I am sending the Integer object in line1. So while evaluating the expression JVM won't call toString() method for 'i' in line1.
When i called explicitly , i got the expected output.

rami


In line 1 the Integer object's toString method isn't called at all, that's where your confusion comes in. Rather that Integer object's intValue is called, which you'll see if you put a breakpoint in the code and step through it.
That's boxing and unboxing, the sneakiness of treating an Integer as an int where appropriate and an int as an Integer.

In line 2 it's a String, because you called toString, no sneaky boxing for Strings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic