Is there's any difference between char literals '\"' and '"' ?
            Asked
            
        
        
            Active
            
        
            Viewed 1,493 times
        
    2
            
            
         
    
    
        polygenelubricants
        
- 376,812
- 128
- 561
- 623
 
    
    
        user388989
        
- 61
- 1
- 1
- 3
- 
                    Ummm ... it would have taken 30 seconds to test it yourself. – Stephen C Jul 11 '10 at 22:06
1 Answers
9
            
            
        There is absolutely no difference. The two char are ==.
System.out.println('\"' == '"'); // prints "true"
Strictly speaking it's not necessary to escape a double quote in a char literal, but it doesn't change this fact that \" denotes the double quote character \u0022.
References
String analog
We also have the analogous situation for String literals:
System.out.println("\'".equals("'")); // prints "true"
In fact, we can even go a step further and use == for reference equality:
System.out.println("\'" == "'"); // prints "true"
The second snippet proves that the two string literals are really equal, and therefore subject to string interning at compile-time.
References
- JLS 3.10.5 String Literals
String literals --or, more generally, strings that are the values of constant expressions-- are "interned" so as to share unique instances, using the method String.intern.
Related questions
- How do I compare strings in Java?
-  Java String.equalsversus==
- Where do Java and .NET string literals reside?
-  When “” == sisfalsebut“”.equals( s )istrue
Summary
- A single-quote in a charliteral MUST be escaped- Because charliteral is quoted in single-quotes
 
- Because 
- A double-quote in a Stringliteral MUST be escaped- Because Stringliteral is quoted in double-quotes
 
- Because 
- It doesn't hurt to escape, even when it's not necessary
- Go with what's most readable
 
 
    
    
        Community
        
- 1
- 1
 
    
    
        polygenelubricants
        
- 376,812
- 128
- 561
- 623