user3758749
user3758749

Reputation: 117

In Java, why does System.out.println("\" \\"); gives " \ as output

In Java when i run System.out.println("\" \\"); I get output as :

" \

Can you please explain in detail, why this is happening?

Upvotes: 2

Views: 13164

Answers (8)

supraja
supraja

Reputation: 1

The standard definition according to oracle is as follows: A character preceded by a backslash \ is an escape sequence and has special meaning to the compiler.The following table shows the Java escape sequences

EscapeSequence Description

  1. \" Insert a double quote character in the text at this point.
  2. \\ Insert a backslash character in the text at this point.

By following the above description in our case for System.out.println("\" \\"), \" would be replaced with a double " quote character and \\ would be replaced with a (single backslash) \ character.

Hence output printed will be " \

Hope this helps.

Upvotes: 0

CMPS
CMPS

Reputation: 7769

backslash is a special character in JAVA and many other programming languages, one of its use is to escape characters in certain situation.

For example:

If you want to print a string containing double quotes like: How are you "Bob" ?
Printing this using System.out.println("How are you "Bob" ?"); will not work because you are closing the quotes just before the word Bob. Therefore, a character was used to deal with such situation so one can print double quotes inside a string:

System.out.println("How are you \"Bob\" ?");

Moreover, since we've agreed above that \ escapes the double quotes, if you want to print a single backslash inside a string, doing this System.out.println("\"); will open the string but will escape the second double quotes which will result in an error because the string was not closed. To fix this, you need to escape the backslash like this: System.out.println("\");

Other interesting uses of \:

\n character to return to a new line
\t character to insert a tab

More about escape character can be found on Wikipedia

Upvotes: 4

HJK
HJK

Reputation: 1382

These are called escape sequences. All the escape sequences start with \ (backward slash) character (e.g., \n, \t etc.,). Here \n, \t has special meaning to Java like line break and tab space respectively. Similarly " (double quote) has a special meaning saying that termination of string literals in Java. Instead of making " as a string literal terminator, we need to tell java compiler to treat it as a special sequence. Hence we use these escape sequences like \\ (for backward slash), \' (single quote), \r (carriage return) etc.,

Thanks, JK

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201419

Yes. You are escaping two characters,

String s = "\" \\";

uses the single back-slash to escape first the double quote and then a backslash. So you get,

" \

You might also try

System.out.println(s.length());

Which would tell you "3". Because you have a String of '"', ' ' and '\'

Escape Sequences are explained in The Java Tutorial: Characters, which also allows Unicode characters,

System.out.println("\u03A9");

Will output a one character String that equals

 Ω

Upvotes: 1

ngrashia
ngrashia

Reputation: 9894

System.out.println("\" \");  
System.out.println("   --> String Open
                   \"  --> Double Quote character escaped using backslash
                   \\  --> Backslash itself as a character escaped using backslash
                   "); --> String Close

will give you output as "\

For the list of escaped characters, You can find that here.

  • \t Insert a tab in the text at this point.
  • \b Insert a backspace in the text at this point.
  • \n Insert a newline in the text at this point.
  • \r Insert a carriage return in the text at this point.
  • \f Insert a formfeed in the text at this point.
  • \' Insert a single quote character in the text at this point.
  • \" Insert a double quote character in the text at this point.
  • \\ Insert a backslash character in the text at this point.

Upvotes: 3

AppleBud
AppleBud

Reputation: 1541

This is because when you put a \ before a special character in java, \ tells the JVM that it is not a special character, rather it is a part of a String. So in your case, when you put a \ before " , it prints a double quote(") and when you again put \\ , it prints a slash (\). If you want to know more about this, you can go through the inside of Java and how the special characters are handled in java.

Hope it helps.

Upvotes: 0

Mateusz Odelga
Mateusz Odelga

Reputation: 354

In addition to comments of my precursors, you can check it in Oracle's Java Tutorial, list of escape sequences.

http://docs.oracle.com/javase/tutorial/java/data/characters.html

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Because you escape double quotes ("") with a backslash (\) and also a backslash with a backslash.

Upvotes: 4

Related Questions