• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between '\n' and '\r'

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
r kanth
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opps sorry

Stephan
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic