Solved

Escape Sequence  in java

Posted on 2014-01-10
9
617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 100 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
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!

 
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 400 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

628 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