General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.
Use single quotes for literal char s, double quotes for literal String s, like so: char c = 'a'; String s = "hello"; They cannot be used any other way around (like in Python, for example).
There is really no difference in the end between using single or double quotes, meaning they both represent a string in the end. The system doesn't really care which one you use (but you might?). No need to escape the other character within a string.
Double quotes are mainly used to indicate a string. When we print any string, the double quotes are not printed but only the value inside them is printed.
Use single quotes for literal char
s, double quotes for literal String
s, like so:
char c = 'a';
String s = "hello";
They cannot be used any other way around (like in Python, for example).
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 = '"';
Let's consider this lines of codes (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
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With