Is There a Difference Between Single and Double Quotes in Java

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

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).

Weird behaviour between double quote and single quote in java

Single quotes are used for the char type. Since this is a numerical type, doing int + char will result in an int value.

A space character (' ') has a numerical value of 32, so:

1 + ' ' == 33; 

c:out/ single quote vs double quote. What is the difference?

There is no difference, either is fine. the reason both are valid is so that " or ' can be used inside the expression. In that case you would use the other one to contain the expression. javascript and html also follow this convention.

example:

<c:out value='${expression.getValue("example")}'/>

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.

In Java, should I escape a single quotation mark (') in String (double quoted)?

You don't need to escape the ' character in a String (wrapped in "), and you don't have to escape a " character in a char (wrapped in ').

vs vs ' ' ' in Groovy .When to Use What?

I would confuse you even more, saying, that you can also use slash /, dolar-slash $/ and triple-double quotes """ with same result. =)

So, what's the difference:

  1. Single vs Double quote: Most important difference. Single-quoted is ordinary Java-like string. Double-quoted is a GString, and it allows string-interpolation. I.e. you can have expressions embedded in it: println("${40 + 5}") prints 45, while println('${ 40 + 5}') will produce ${ 40 + 5}. This expression can be pretty complex, can reference variables or call methods.
  2. Triple quote and triple double-quote is the way to make string multiline. You can open it on one line in your code, copy-paste big piece of xml, poem or sql expression in it and don't bother yourself with string concatenation.
  3. Slashy / and dollar-slashy $/ strings are here to help with regular expressions. They have special escape rules for '\' and '/' respectfully.

As @tim pointed, there is a good official documentation for that, explaining small differences in escaping rules and containing examples as well.

Most probably you don't need to use multiline/slashy strings very often, as you use them in a very particular scenarios. But when you do they make a huge difference in readability of your code!

What's the difference of strings within single or double quotes in groovy?

Single quotes are a standard java String

Double quotes are a templatable String, which will either return a GString if it is templated, or else a standard Java String. For example:

println 'hi'.class.name    // prints java.lang.String
println "hi".class.name // prints java.lang.String

def a = 'Freewind'
println "hi $a" // prints "hi Freewind"
println "hi $a".class.name // prints org.codehaus.groovy.runtime.GStringImpl

If you try templating with single quoted strings, it doesn't do anything, so:

println 'hi $a'            // prints "hi $a"

Also, the link given by julx in their answer is worth reading (esp. the part about GStrings not being Strings about 2/3 of the way down.

Difference between double quotes and single quotes in Rust

The short answer is it works similarly to java. Single quotes for characters and double quotes for strings.

let a: char = 'k';
let b: &'static str = "k";

The b'' or b"" prefix means take what I have here and interpret as byte literals instead.

let a: u8 = b'k';
let b: &'static [u8; 1] = b"k";

The reason strings result in references is due to how they are stored in the compiled binary. It would be too inefficient to store a string constant inside each method, so strings get put at the beginning of the binary in header area. When your program is being executed, you are taking a reference to the bytes in that header (hence the static lifetime).

Going further down the rabbit hole, single quotes technically hold a codepoint. This is essentially what you might think of as a character. So a Unicode character would also be considered a single codepoint even though it may be multiple bytes long. A codepoint is assumed to fit into a u32 or less so you can safely convert any char by using as u32, but not the other way around since not all u32 values will match valid codepoints. This also means b'\u{x}' is not valid since \u{x} may produce characters that will not fit within a single byte.

// U+1F600 is a unicode smiley face
let a: char = '\u{1F600}';
assert_eq!(a as u32, 0x1F600);

However, you might find it interesting to know that since Rust strings are stored as UTF-8, codepoints over 127 will occupy multiple bytes in a string despite fitting into a single byte on their own. As you may already know, UTF-8 is simply a way of converting codepoints to bytes and back again.

let foo: &'static str = "\u{1F600}";
let foo_chars: Vec<char> = foo.chars().collect();
let foo_bytes: Vec<u8> = foo.bytes().collect();

assert_eq!(foo_chars.len(), 1);
assert_eq!(foo_bytes.len(), 4);

assert_eq!(foo_chars[0] as u32, 0x1F600);
assert_eq!(foo_bytes, vec![240, 159, 152, 128]);

Getting data between single and double quotes (special case)

I would try without capture quote type/lookahead/backref to improve performance. See this question for escaped characters in quoted strings. It contains a nice answer that is unrolled. Try like

'[^\\']*(?:\\.[^\\']*)*'|"[^\\"]*(?:\\.[^\\"]*)*"

As a Java String:

String regex = "'[^\\\\']*(?:\\\\.[^\\\\']*)*'|\"[^\\\\\"]*(?:\\\\.[^\\\\\"]*)*\"";

The left side handles single quoted, the right double quoted strings. If either kind overbalances the other in your source, put that preferably on the left side of the pipe.

See this a demo at regex101 (if you need to capture what's inside the quotes, use groups)



Related Topics



Leave a reply



Submit