/* * File: NewLines.java * Desc: Demonstrate newlines in Java * Vers: 1.0.0 dgg - original coding */ /** * New lines in Java are handled differently than in C. * * New line handling is system dependent. In Windows it is \r\n, in * Unix, Linux, and OS X it is \n and in MAC OS 9 and earlier it was \r. * There are additional choices but that covers the present large deployments. * * C handles this by grabbing \n in String IO and mapping it correctly. * * Java treats \n in strings as just another character so \n is not going * to get a newline in Windows (but works for many other operating systems). * * Java's first approach to handling the issue is to provide methods for * appending newlines (System.out.println with newline as well as * System.out.print without newline.) * * Java "knows" the correct newline sequence (and many other system * dependencies) through configuration * settings which are accessible through * the System class. * * In this case, the property line.separator has the correct string for * providing a "newline" behavior on the present platform. */ public class NewLines { /** * Short demo program */ public static void main(String[] args) { // Get line separator (newline) for present system String lineSeparator = System.getProperty("line.separator"); System.out.println("Single Spacing"); System.out.println("Double Spacing"); System.out.println(""); System.out.print("10 spacing"); for ( int i = 0; i < 10; i++) System.out.print(lineSeparator); System.out.println("first line" + lineSeparator + "second line"); } }