aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why '\n' is not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why Watch "Why New topic
Author

Why '\n' is not working?

Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29


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
Marshal

Joined: Mar 22, 2005
Posts: 41487
    
  51
Nice one. A clue is the fact that the code says "100", while the output says "110".


Ping & DNS - my free Android networking tools app
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
O,thanks .
I think I got it,the char cast to the int.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
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 .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
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
Marshal

Joined: Mar 22, 2005
Posts: 41487
    
  51
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

Joined: Nov 20, 2007
Posts: 192
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

Joined: Mar 25, 2008
Posts: 8
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

Joined: Feb 20, 2007
Posts: 88
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

Joined: Nov 20, 2007
Posts: 192
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why '\n' is not working?