wood burning stoves 2.0*
The moose likes Java in General and the fly likes Newline Vs Carriage return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Newline Vs Carriage return" Watch "Newline Vs Carriage return" New topic
Author

Newline Vs Carriage return

omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
In StringTokeinzer class, default delimiters are " newline" and "carriage return".....whats the difference between the two ?


Thanks and Regards
Omkar Patkar (SCJP 1.4)[url]http://javacollectionsnotes.blogspot.com[/url] | [url]http://omkar-myscjpexp.blogspot.com[/url]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Some OS's use CR to separate lines, some use LF, some use both. Having both as delimiters helps to make Java OS independent.


Joanne
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Thanks!...... but whats CR and LF....what do they stand for ?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
CR - carriage return
LF - line feed (also known as newline)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you can look at a text file with a hex editor you'll see that CR and LF are actually 1-byte characters, x'0A' and x'0D' I think, tho I forget which is which. It's been that long since I had to know! The last time I cared at all I was parsing a file one character at a time and threw away any carriage returns and I just used the Java \r to find them.

There are a bunch of other special characters in the ASCII standard, eg "formfeed" and "bell" for printers. Tab \t is the only other one I've bothered with in Java.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
In old technology,
a carriage return on a printer moved the print position to the begining of the current line but stayed on the same line.
A newline moved the print position to a new line but did not change the column position. Some printers can print left-right and right-left. So you print a line left-right, go to a new line and print right-left.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11447
    
  16

and the CR was used for bold or underlining (assuming the printer only worked L-R) the printers could ONLY print one character at a time. so you would print the line sans-underscores, use a carriage return, then move the printer head to the correct spot, and print the underline character. For bold, you'd print the same word again


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
LF, Line Feed is the Unicode character 0x000A (decimal 10), and is often erroneously referred to as a newline character.

CR, Carriage Return is the Unicode character 0x000D (decimal 13).

http://jqa.tmorris.net/GetQAndA.action?qids=62&showAnswers=true
[ September 23, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Deepak Kaimal
Greenhorn

Joined: Aug 30, 2005
Posts: 13
The naming seems to have arisen from typewriters before printers. If you have ever used one of those (ancient) devices, you would know that once you type all the way to the right end of the paper, you need to do two things
1. Push the paper up one line (line feed)
2. Pull the carriage all the way to the left so that you are now positioned to type from the left edge (carriage return)

The behaviour is similar for printers (or even typing into text files). I guess the analogy would be that in a text editior, when you press Enter, should the cursor come to the line below the current line in the same column (which would mean you have to hit the "Home" key too), or should it come to the first column in the line below the current line.

Okay, so that didnt make much sense, but it sounds like a good analogy to me if someone can put it into better words
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by omkar patkar:
In StringTokeinzer class, default delimiters are " newline" and "carriage return".....whats the difference between the two ?


They are different unicode character values. Nothing more can be assumed. They are just numbers. Typically, however, a newline character implies a line feed and a carriage return when sent to an character output device. Carriage return strictly means return to the left margin. It's from a device that was used in the early part of the last century. I saw one in the Smithsonian.
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
I suggest that this is an appropriate place to be a little pedantic, and to make sure we are clearly separating (excuse the pun) line terminators and token delimiters. The initial question was a little misleading, for those not familiar with the StringTokenizer class, in stating that default delimiters are "newline" and "carriage return". From the API doc for StringTokenizer(String str):
The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.

The newline and carriage return are just two of the default delimiters. The point I want to emphasize (for Java newbies like myself) is that in the context of StringTokenizer these are token delimiters, not line terminators.

It is probably reasonable to assume that most (but not all) Strings passed to StringTokenizer begin life being read as lines. In such cases, any newlines or carriage returns in the input stream will be read (and discarded) as line terminators, and will never get as far as StringTokenizer.
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Hi all,

I also am confused with the '\r' special character.



produces ITT as result. Why is this so?


SCJP, SCWCD, SCBCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Please don't reopen a 4-year old thread.

Try changing that to got \rIT, then you will see it is in fact printing got, then IT over the top of it, and you should get ITt.

This is because the \r (except on older Macs) simply takes the cursor to the start of the line, not to a new line. This behaviour varies from one operating system to another, so when I tried it (with print, not println) on Linux I got
[Campbell@localhost java]$ java CRDemo
IT[Campbell@localhost java]$
I am old enough to remember the TTY (Teletype); we had to use CR to move the head back to the left, then LF to move the paper one line up.
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

GOT IT!

Thanks a lot!
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Campbell Ritchie wrote:Please don't reopen a 4-year old thread.

sorry to wake a sleeping thread; when I had posted a previous thread I had been referred to an old thread; so this time I thought it was better to check the existing ones before I posted a new one. Really sorry!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Apologies accepted, and well done having GOT IT
Suyash Soni
Greenhorn

Joined: Jun 09, 2013
Posts: 6
hey guys see the image that i've attached.



[Thumbnail for Carriage return.png]

Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Suyash Soni wrote:hey guys see the image that i've attached.


With what object?

P.S. It is considered bad form to hijack a thread especially when it is this old!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newline Vs Carriage return