Solved

Escape Sequence  in java

Posted on 2014-01-10
9
610 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

734 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