Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Escape Sequence  in java

Posted on 2014-01-10
9
Medium Priority
?
634 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 400 total points
ID: 39773397
When you are writing your own printer application
0
 
LVL 35

Expert Comment

by:girionis
ID: 39773624
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
ID: 39816119
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:girionis
ID: 39817854
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
 
LVL 7

Author Comment

by:gudii9
ID: 39818657
any simple example program to understand this. Please advise
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39819765
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
ID: 39819850
\r\n

why we need both

\n linefeed
\r carriage return

please advise
0
 
LVL 36

Accepted Solution

by:
mccarl earned 1600 total points
ID: 39819884
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
ID: 39820207
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

972 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