Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pl.explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pl.explain" Watch "Pl.explain" New topic
Author

Pl.explain

bunny
Greenhorn

Joined: Jun 21, 2001
Posts: 2
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Pl.explain