Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Escape character for single quotes. Is it really required in String?

12 views
Skip to first unread message

Mausam

unread,
Feb 7, 2012, 6:14:32 AM2/7/12
to
Hello,

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089
defines escape character for single character as well.

But single quote works fine in String without any escape character.

public class Class1 {

private static String str = "I have a single quote. That's it";
private static String str1 = "I have a single quote. That\'s it";
public static void main(String[] args) {
System.out.println(str);
System.out.println(str1);
}
}

How the two string is different? And when should I use escape
character for single quote?

Mausam

unread,
Feb 7, 2012, 6:18:33 AM2/7/12
to
Correction :http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089
defines escape character for single quote as well.

Leif Roar Moldskred

unread,
Feb 7, 2012, 6:30:16 AM2/7/12
to
Mausam <mausa...@gmail.com> wrote:

> How the two string is different? And when should I use escape
> character for single quote?

In character literals.

char apostrophe = '\'';

--
Leif Roar Moldskred

Mausam

unread,
Feb 7, 2012, 7:29:08 AM2/7/12
to
On Tuesday, 7 February 2012 17:00:16 UTC+5:30, Leif Roar Moldskred wrote:
> Mausam <mausa...@gmail.com> wrote:
>
> > How the two string is different? And when should I use escape
> > character for single quote?
>
> In character literals.
>
> char apostrophe = '\'';

Thanks. For char it makes sense. But then my understanding that this escape character does not make any difference when used in String is correct.


>
> --
> Leif Roar Moldskred

Knute Johnson

unread,
Feb 7, 2012, 1:28:36 PM2/7/12
to
and the Strings are equal;

public class Class1 {

private static String str = "I have a single quote. That's it";
private static String str1 = "I have a single quote. That\'s it";
public static void main(String[] args) {
System.out.println(str);
System.out.println(str1);
System.out.println(str.equals(str1));
}
}


C:\Documents and Settings\Knute Johnson>java Class1
I have a single quote. That's it
I have a single quote. That's it
true

--

Knute Johnson

Lew

unread,
Feb 7, 2012, 2:11:10 PM2/7/12
to
On Tuesday, February 7, 2012 3:14:32 AM UTC-8, Mausam wrote:
> http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089

That's the wrong document! Why are you citing ancient information? Get current.
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html

> defines escape character for single character as well.
>
> But single quote works fine in String without any escape character.
>
> public class Class1 {
>
> private static String str = "I have a single quote. That's it";
> private static String str1 = "I have a single quote. That\'s it";
> public static void main(String[] args) {
> System.out.println(str);
> System.out.println(str1);
> }
> }
>
> How the two string is different?

Why do you claim that they're different?

> And when should I use escape character for single quote?

When you need to in order to make the expression mean what you intend.

--
Lew
0 new messages