• Post Reply Bookmark Topic Watch Topic
  • New Topic

\n or line.separator?  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help settle a small argument with my boss.

What is the difference between the \n character and the line.separator system property in Java? When might it make a difference which one you use?
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess "\n" is more unix specific refer the java docs of System class.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

releated details can found below:
[ UD: removed links to copyrighted material ]
[ August 03, 2007: Message edited by: Ulf Dittmer ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The \n character is a single character which is called "new line," usually, although "linefeed" is more accurate. The line separator varies from computer to computer. If you run this little class which finds all the system properties, it prints out the line separator, which usually looks like this

and later on prints which characters the line separator is made up of.You will get different results on Windows and Linux. The lines separator is added into Strings if you use the String.format method with the %n format tag; always use %n rather than \n. The Java Tutorial suggests %n is better than \n.
I ran my PropertiesTest program and this is part of the output
[Campbell@dhcppc0 disk-1]$ java trivia.PropertiesTest
-- listing properties --
. . .
path.separator=:
. . .
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
os.name=Linux
. . .
java.class.version=50.0
sun.management.compiler=HotSpot Client Compiler
os.version=2.6.20-1.2944.fc6
. . .
java.class.path=.
. . .
java.version=1.6.0_01
. . .
file.separator=/
. . .

Line separator: "line.separator": a

a = LF or \n c = FF or \f d = CR or \r

/usr/java/jdk1.6.0_01/bin/:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/Campbell/bin
[Campbell@dhcppc0 disk-1]$

I think you will get ad for line separator on Windows.
So which side of the argument are you on with your boss?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, in Java, \n always means the single character LF (line feed)? Yea! I win the argument.

I believe that in some other languages, \n means the platform-specific new-line character(s). I think it does in C, for instance.

Extra
But even in Java, the waters are a little muddy, I hear. I am told that System.out translates \n into platform-specific new-line character(s). Also that Swing (e.g. JTextArea) doesn't understand platform-specific new-line and always wants \n.
[ May 01, 2007: Message edited by: Peter Chase ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I win the argument. Please pour 1 pint of real beer into your modem and I expect to be able to drink to your success later.

I tried my little program on the other half of my computer (running Windows XP rather than Linux) and it printed this
A:\>java trivia.PropertiesTest
-- listing properties --
. . .
java.vm.version=1.6.0_01-b06
. . .
path.separator=;
. . .
line.separator=

os.name=Windows XP
. . .

Line separator: "line.separator": d a

a = LF or \n c = FF or \f d = CR or \r
. . .
A:\>

Note the differences from my trial on Linux earlier.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think \n means platform-specific anything. I think what happens is that use of \n causes a linefeed character to be printed, and the OS can usually interpret that as a new line.


Anybody else got better ideas? [edit: Add this bit]I might be very mistaken, though. Not sure[/edit]
[ May 01, 2007: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That Java in a Nutshell link was very useful, thank you, Srikanth Ramu.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Peter]: I am told that System.out translates \n into platform-specific new-line character(s).

No, however when you use println, the ln means it places a platform-specific line terminator at the end of the line. However any additional \n or \r which you put in the test gets passed on without any specific translation. So if you run

on Windows, the result is the same as


Also, since JDK 1.5, all PrintWriters and PrintStreams have a printf() and format() method which accept the special sequence %n and translate it to a platform-specific line separator.

[Campbell]: I don't think \n means platform-specific anything. I think what happens is that use of \n causes a linefeed character to be printed, and the OS can usually interpret that as a new line.

Yup. Many utilities today are designed to treat \n and \r\n as equivalent, so you won't notice a difference. Others are not. For example if you try writing to a file and then opening the file with Notepad, you can see it's as though any lone \n is ignored, while \r\n functions normally.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jim; I see I got it right for once!

Quoted by Jim Yingst
Notepad
Even more reasons to hate that dreadful program!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[Peter]: I am told that System.out translates \n into platform-specific new-line character(s).

No, ... any additional \n or \r which you put in the test gets passed on without any specific translation.


Note the "I am told" in my post. That information came from the same source as the original suggestion that \n meant CR-LF on Windows. I was as sceptical about the former as I was about the latter.

All clear now, I think.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. "No" in this case meant "the person who originally said this is wrong".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!