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

Discussion in 'Java' started by Mausam, Feb 7, 2012.

  1. Mausam

    Mausam Guest

    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, Feb 7, 2012
    #1
    1. Advertising

  2. Mausam

    Mausam Guest

    Mausam, Feb 7, 2012
    #2
    1. Advertising

  3. Mausam

    Mausam Guest

    On Tuesday, 7 February 2012 17:00:16 UTC+5:30, Leif Roar Moldskred wrote:
    > Mausam <> 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
    Mausam, Feb 7, 2012
    #3
  4. Re: Escape character for single quotes. Is it really required inString?

    On 2/7/2012 4:29 AM, Mausam wrote:
    > On Tuesday, 7 February 2012 17:00:16 UTC+5:30, Leif Roar Moldskred wrote:
    >> Mausam<> 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

    >


    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
    Knute Johnson, Feb 7, 2012
    #4
  5. Mausam

    Lew Guest

    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
    Lew, Feb 7, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    13,633
    Oisin
    Mar 24, 2006
  2. Leif B. Kristensen

    escape single and double quotes

    Leif B. Kristensen, Mar 24, 2005, in forum: Python
    Replies:
    7
    Views:
    36,261
    Kent Johnson
    Mar 24, 2005
  3. slomo
    Replies:
    5
    Views:
    1,528
    Duncan Booth
    Dec 2, 2007
  4. Replies:
    10
    Views:
    1,201
    Anno Siegel
    Apr 17, 2006
  5. Henry J.
    Replies:
    16
    Views:
    282
    Peter J. Holzer
    Oct 8, 2008
Loading...

Share This Page