126

Is there a difference between single and double quotes in Java?

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

4 Answers4

180

Use single quotes for literal chars, double quotes for literal Strings, like so:

char c = 'a';
String s = "hello";

They cannot be used any other way around (like in Python, for example).

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • 14
    And, of course, this behavior is borrowed from C (which probably got it somewhere else, I presume). – JesperE Jan 13 '09 at 16:01
  • For me, apostrophes literal automatically casted to `int` in such contexts. So it doesn't need to be `char`. Deservin' some downvotes... simplistic. –  Aug 23 '17 at 15:16
  • @Klaider can you elaborate on that? Would some characters not cast to a char? – ngood97 Mar 26 '21 at 18:29
  • @ngood97 My comment was rather wishing apostrophes automatically adapted to `int` in Java (`int c = 'a';`), but it's outdated. Actually, my ECMAScript dialect has string literal automatically adapting to `Char` (`var ch:Char = "a";`). –  Mar 26 '21 at 19:08
  • Oh so java doesn't in fact cast character literals to int. Which ECMAScript dialect does this syntax you're referring to btw? – ngood97 Mar 26 '21 at 19:21
  • @ngood97 [DoveScript](https://github.com/dovescript/developernetwork/blob/master/demo/Enumerations.md), but the compiler doesn't still have a target and haven't implemented the runtime. –  Mar 27 '21 at 10:59
42

A char is a single UTF-16 character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar.

A char literal is either a single one character enclosed in single quote marks like this

char myCharacter = 'g'; 

or an escape sequence, or even a unicode escape sequence:

char a = '\t';    // Escape sequence: tab
char b = '\177'   // Escape sequence, octal.
char c = '\u03a9' // Unicode escape sequence. 

It is worth noting that Unicode escape sequences are processed very early during compilation and hence using '\u00A' will lead to a compiler error. For special symbols it is better to use escape sequences instead, i.e. '\n' instead of '\u00A' .

Double quotes being for String, you have to use a "double quote escape sequence" (\") inside strings where it would otherwise terminate the string.
For instance:

System.out.println("And then Jim said, \"Who's at the door?\"");

It isn't necessary to escape the double quote inside single quotes.
The following line is legal in Java:

char doublequote = '"';
Vlad Gudim
  • 23,397
  • 16
  • 69
  • 92
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
21

Let's consider this lines of code (Java):

System.out.println("H"+"A"); //HA
System.out.println('H'+'a'); //169
  1. First line is concatenation of H and A that will result in HA (String literal)

  2. Second we are adding the values of two char that according to the ASCII Table H=72 and a=97 that means that we are adding 72+97 it's like ('H'+'a').

  3. Let's consider another case where we would have:

    System.out.println("A"+'N');//AN

In this case we are dealing with concatenation of String A and char N that will result in AN.

starball
  • 20,030
  • 7
  • 43
  • 238
Eddy Bayonne
  • 2,448
  • 1
  • 17
  • 23
5

Single quote indicates character and double quote indicates string..

char c='c';

'c'-----> c is a character

String s="stackoverflow";

"stackoverflow"------> stackoverflow is a string(i.e collection if characters)

Horse_1995
  • 227
  • 5
  • 14