Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pl.explain

 
bunny
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output for these 2 different set of code is the same,why?
1.)System.out.println("*\n**\n***\n****\n*****");
2.)System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bunny:
The output for these 2 different set of code is the same,why?
1.)System.out.println("*\n**\n***\n****\n*****");
2.)System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");

When you use the method println, the JVM automatically appends a line termination character to the end of each line of output. That's why each set of stars ends up on its own line.
In the first line, you're putting those in yourself. However, I believe the newline character \n is specific to Windows. If you were to run this on a Mac, I believe these two lines would give you different output (someone please correct me if I'm wrong here). If you want to use a system independent line separator, check out the System.getProperty method in the API.
Corey
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bunny,
Welcome to Javaranch, a friendly place for Java greenhorns
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our rules. Thank you for your cooperation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic