• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why '\n' is not working?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O,thanks .
I think I got it,the char cast to the int.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic