A newline
denotes the end of a line and the beginning of a new line. It is also known as the following:
Different operating systems use different character notations to denote a newline
, with one or two control characters.
Operating System | End of Line Character |
---|---|
Windows | \r\n |
Linux | \n |
Mac | \n |
We can use the control characters, \n
in Unix and \r\n
in Windows, as part of strings to denote the newline.
The problem with this approach would be the portability of the program.
public class Main {public static void main(String[] args) {System.out.println("Hello" + "\n" + "Educative");}}
line.separator
propertyThe safest and recommended approach to use is the line.separator
property using the System.getProperty()
method in Java.
public class Main {public static void main(String[] args) {System.out.println("Hello" + System.getProperty("line.separator") + "Educative");}}
System.lineSeparator()
methodAnother approach to use is the lineSeparator()
static method of the System
class.
public class Main {public static void main(String[] args) {System.out.println("Hello" + System.lineSeparator() + "Educative");}}