Core Java

Prefer System.lineSeparator() for Writing System-Dependent Line Separator Strings in Java

JDK 7 introduced a new method on the java.lang.System class called lineSeparator(). This method does not expect any arguments and returns a String that represents “the system-dependent line separator string.” The Javadoc documentation for this method also states that System.lineSeparator() “always returns the same value – the initial value of the system property line.separator.” It further explains, “On UNIX systems, it returns “\n“; on Microsoft Windows systems it returns “\r\n“.”

Given that a Java developer has long been able to use System.getProperty(“line.separator”) to get this system-dependent line separator value, why would that same Java developer now favor using System.lineSeparator instead? JDK-8198645 [“Use System.lineSeparator() instead of getProperty(“line.separator”)”] provides a couple reasons for favoring System.lineSeparator() over the System.getProperty(String) approach in its “Description”:

A number of classes in the base module use System.getProperty(“line.separator”) and could use the more efficient System.lineSeparator() to simplify the code and improve performance.

As the “Description” in JDK-8198645 states, use of System.lineSeparator() is simpler to use and more efficient than System.getProperty("line.separator"). A recent message on the core-libs-dev mailing list provides more details and Roger Riggs writes in that message that System.lineSeparator() “uses the line separator from System instead of looking it up in the properties each time.”

The performance benefit of using System.lineSeparator() over using System.getProperty("line.separator") is probably not all that significant in many cases. However, given its simplicity, there’s no reason not to gain a performance benefit (even if tiny and difficult to measure in many cases) while writing simpler code. One of the drawbacks to the System.getProperty(String) approach is that one has to ensure that the exactly matching property name is provided to that method. With String-based APIs, there’s always a risk of spelling the string wrong (I have seen “separator” misspelled numerous times as “seperator”), using the wrong case, or accidentally introducing other typos that prevent exact matches from being made.

The JDK issue that introduced this feature to JDK 7, JDK-6900043 (“Add method to return line.separator property”), also spells out some benefits in its “Description”: “Querying the line.separator value is a common occurrence in large systems. Doing this properly is verbose and involves possible security failures; having a method return this value would be beneficial.” Duplicate JDK-6264243 (“File.lineSeparator() to retrieve value of commonly used ‘line.separator’ system property”) spells out advantages of this approach with even more detail and lists “correctness”, “performance”, and “ease of use and cross-platform development” as high-level advantages. Another duplicate issue, JDK-6529790 (“Please add LINE_SEPARATOR constant into System or some other class”), makes the point that there should be a “constant” added to “some standard Java class, as String or System,” in a manner similar to that provided for file separators by File.pathSeparator.

One of the messages associated with the JDK 7 introduction of System.lineSeparator() justifies it additions with this description:

Lots of classes need to use System.getProperty("line.separator"). Many don’t do it right because you need to use a doPrivileged block whenever you read a system property. Yet it is no secret – you can divine the line separator even if you have no trust with the security manager.

An interesting side note related to the addition of System.lineSeparator() in JDK 7 is that the Javadoc at that time did not indicate that the method was new to JDK 7. JDK-7082231 (“Put a @since 1.7 on System.lineSeparator”) addressed this in JDK 8 and two other JDK issues (JDK-8011796 and JDK-7094275) indicate that this was desired by multiple Java developers.

The introduction of System.lineSeparator() was a very small enhancement, but it does improve the safety and readability of a relatively commonly used API while not reducing (and, in fact, while improving) performance.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Prefer System.lineSeparator() for Writing System-Dependent Line Separator Strings in Java

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button