aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between '\n' and '\r' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between Watch "Difference between New topic
Author

Difference between '\n' and '\r'

r kanth
Greenhorn

Joined: Jul 14, 2004
Posts: 6
Hi all

class Test4{

public static void main(String args[]){
System.out.println("\n Hi this is a test print \r i dont know ");
System.out.println("Can anyone explain me this \r ???");
}
}

Output is

C:\java Test4

i dont know test print
???anyone explain me this

Can anyone plz explain the differece between \n and \r and how do they work and the code too

thank you
sri
Stephan Deve
Greenhorn

Joined: Sep 21, 2004
Posts: 13
Hello,
if i run this code my output is:


\n stands for Linefeed(LF) aka Newline(NL)
\r stands for Carriage Return (CR)

Hope this helps you a little bit...

greetings
Stephan dV
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In general, \n (new line) accomplishes what was originally done using printer commands \r\f (a combination of a carriage return and a form feed). Although \r now seems to behave the same as \n in some contexts, the safe approach is to use \n.

Compare the following entries at wikipedia.org (which offer a bit of Unix and C history)...

Carriage return: http://en.wikipedia.org/wiki/Carriage_return
Line (form) feed: http://en.wikipedia.org/wiki/Line_feed
Newline: http://en.wikipedia.org/wiki/Newline
[ September 21, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
r kanth
Greenhorn

Joined: Jul 14, 2004
Posts: 6
Thank you

Mark and Stevan

Stevean i still get the same output when i compiled

i am using winxp and jdk1.4.2 i also compiled using netbeans id

i am getting same out put

thank you again
r kanth
Greenhorn

Joined: Jul 14, 2004
Posts: 6
Opps sorry

Stephan
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not sure whether your question was really answered, so...

The return character \r is returning to the beginning of the same line, then overwriting whatever was there.

Consider the line "\n Hi this is a test print \r i dont know ".

After starting a new line, the first part, " Hi this is a test print ", has 25 characters (counting the space before and after).

Then the "carriage" returns to the beginning of that same line and overwrites the first 13 characters with, " i dont know ". The remaining 12 characters of the original line (" test print ") remain after the 13 that were overwritten.

So the result is " i dont know test print ".

Yes, I get these same results. I'm using Java 1.4.2 with the simple Command Prompt in Windows XP Home Edition.

Interestingly, when I try to use the form feed character (\f), it just displays the symbol for Venus -- a small circle with a cross beneath it. So now I have a question...
[ September 21, 2004: Message edited by: marc weber ]
Stephan Deve
Greenhorn

Joined: Sep 21, 2004
Posts: 13
I tested the program with Eclipse 3.0 on Windows 2000 with 1.4.2, on Windows NT with WSAD 5.1 with 1.4 and with Eclipse 3.0 on Suse Linux 9.0, again 1.4.2.
I always get the same output. Looks like Eclipse does something strange with the \r...

greetings
Stephan dV
r kanth
Greenhorn

Joined: Jul 14, 2004
Posts: 6
I too get the same output as u get marc using the \f

ya it looks like \r is overwriting the characters but i still wonder

with the output of the Stephan i think \r is behaving like \n

i think it depends on the compiler may be

not sure though
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Now that I'm at work again, I tested this same code with Java 1.3.1 on Windows 2000. Same result: The \r overwrites the beginning of the same line as expected, but \f displays -- at least in Windows' Command Prompt -- as the Venus symbol.

I suspect this might have to do with what fonts are available to the system. I glanced at some Unicode and ASCII references, but I don't have any experience with this, and nothing jumped out at me...

Unicode: http://www.unicode.org/
ASCII: http://www.asciitable.com/

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Apparently, the Venus character is an ACSII display equivalent to the printer command, form feed (\f).

Ref: http://home.att.net/~gobruen/progs/ascii.html

Also, in searching a Sun forum, I found that the Windows Command Prompt is capable of displaying only the first 128 (non-extended) ASCII characters, so anything beyond \u007f will default to the question mark.
[ September 22, 2004: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between '\n' and '\r'
 
Similar Threads
Please Explain
this
; after the if statement
John Hunt Question 62
Explaination reg inner classes