Solved

Escape Sequence  in java

Posted on 2014-01-10
9
567 Views
Last Modified: 2014-01-30
Hi,

I am going through below link

http://www.avajava.com/tutorials/lessons/what-are-the-java-escape-sequences.html

I understood to represent single quote within a character literal etc we use as below

\' single quote
\" double quote


I have not understood what would be the practical purpose of below Escapte sequences. I neve come across them in any character literal i saw till now.
Any example to understand this concept.

Escape Sequence Description
\b backspace
\f formfeed
\n linefeed
\r carriage return
\t horizontal tab

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 100 total points
Comment Utility
When you are writing your own printer application
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
In computing everything you see (or don't see) on the screen is a character. The space is a character, the new line is a character, the tab is a character, etc. But how do you output these special characters in Java? You simply use the escape sequences above.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
When you are writing your own printer application


how do you output these special characters in Java?

I am not clear on above lines. Can you please elaborate. when do we want to output and when we want to write own printer application. Any such applications available to refer. Please advise
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
I will give you a simple example. How do you output a newline? There is any "normal" character for a new line, so you use a special character.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Author Comment

by:gudii9
Comment Utility
any simple example program to understand this. Please advise
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Ok, maybe these examples will give you a better idea...
System.out.println("This is one line");
System.out.println("This is a second line");

Open in new window

is basically identical to this (notice that we are using .print this time instead of .println)...

System.out.print("This is one line\r\nThis is a second line\r\n");

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
\r\n

why we need both

\n linefeed
\r carriage return

please advise
0
 
LVL 35

Accepted Solution

by:
mccarl earned 400 total points
Comment Utility
In fact, in some/most cases you don't "need" both, but I wrote that just as a hangover since I am more a Windows programmer than a Unix programmer and Windows standard newline is both CR & LF. On Unix system the standard is just the LF (\n).

The reason for both goes back to the old days of type writers, \n or linefeed "feeds" the page up by a line and \r or carriage return "returns the carriage (or type head) back to the first column of the page". Both of these were necessary on a typewriter to start typing at the start of the next line, so that sort of continued over to computers early on.

But I've just done a small test using the above code I gave, and at least with my JVM on my system, I could use \r or \n or \r\n to get the same output in each case, so at least in terms of output to the console you probably don't need both. But definitely, in other situations, such as communicating with hardware or other computer systems or non-Java apps, the correct newline might be more crucial.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
why we need both

\n linefeed
\r carriage return

It depends on what OS you are running on. If you want to be truly neutral you will need to use

System.getProperty("line.separator")

Open in new window


so mccarl's example becomes

System.out.print("This is one line"+System.getProperty("line.separator")+"This is a second line"+System.getProperty("line.separator"));

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now