Solved

Escape Sequence  in java

Posted on 2014-01-10
9
574 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
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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

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

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 35

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Image decoding from Camera 3 72
Java / Linux and Regular Expressions 11 71
ForLoop Example 3 40
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

896 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

17 Experts available now in Live!

Get 1:1 Help Now