tags:

views:

420

answers:

9

Hi

Is there a difference between concatenating strings with '' and ""?

For example, what is the difference between:

String s = "hello" + "/" + "world";

and

String s = "hello" + '/' + "world";

Thanks in advance.

+6  A: 

"." is a String, '.' is a char.

Otávio Décio
+20  A: 

Literals enclosed in double quotes, e.g. "foo", are strings, whereas single-quoted literals, e.g. 'c', are chars. In terms of concatenation behaviour, there'll be no discernible difference.

Nevertheless, it's worth remembering that strings and chars aren't interchangeable in all scenarios, and you can't have a single-quoted string made up of multiple characters.

Rob
To be pedantic, '\n' is an acceptable value for a char (although, strictly, that's just one char, even if represented as two).
Henrik Paul
*nod* - I was mainly trying to point out that the two kinds of quote aren't interchangeable as they are in many other languages.
Rob
+2  A: 

"." is a String consisting of only one character. '.' is a character.

Once you concatenate them together there is no difference.

Darron
There's a slight difference actually, it's marginally faster to append characters than single-character Strings. The difference is so minimal though that it really doesn't matter.
Esko
That's true. But you could argue that telling a novice about this sort of minor issue may put them on the wrong path. :-)
Darron
A: 

Theoretically it is quicker to add a char to a string - Java 6 seems to create StringBuffers under the covers and I remember reading on a Java Performance site that concatenating a char will be marginally quicker.

Fortyrunner
+2  A: 

'' is for character literals.

So you cannot do this:

"Osc" + 'ar' + "Reyes"

Because ar is not a character literal.

In your example it doesn't make much difference because

'/'

is a char literal, and

 "/"

is a String literal containing only one character.

Additionally you can use any UTF character with the following syntax

'\u3c00'

So you can also use:

"Osc" + '\u3c00' + "ar
OscarRyz
:-/ What's wrong with my answer
OscarRyz
One thing you have to remember is that the unicode it converted to the character before compiling. So '\u005c' will be converted to '\' which gives a compile-error.
Peter B. Bock
@Peter: Ooohh!! I didn't knew this. Thanks for the comment: class Un { String s = "Osc" + '\u005c' + "ar"; }
OscarRyz
+1  A: 

Adding a char is about 25% faster than adding a one character String. Often this doesn't matter however, for example

String s = "hello" + "/" + "world";

This is converted to one String by the compiler so no String concatenation/append will occur at run-time in any case.

Peter Lawrey
+1  A: 

You will probably find the following articles useful:

Ben Hardy
+5  A: 
System.out.println('a'+'b'+'c');
> 294
System.out.println("a"+"b"+"c");
> abc

What's happening here is that (char)+(char)=(int) In other words. Use "" for text to avoid surprises.

John Nilsson
+1  A: 

You may just look into the JDK :-)

Given two functions:

public static String concatString(String cs) {
    return "hello" + cs + "world";
}

public static String concatChar(char cc) {
    return "hello" + cc + "world";
}

after examination of the bytecode it boils down to two AbstractStringBuilder.append(String) vs. AbstractStringBuilder.append(char).

Both methods invoke AbstractStringBuilder.expandCapacity(int)) which will allocate a new char[] eventually and System.arraycopy the old content first.

Afterwards AbstractStringBuilder.append(char) just has to put the given char in the array whereas AbstractStringBuilder.append(String) has to check a few constraints and calls String.getChars(int, int, char[], int) which does another System.arraycopy of the appended string.

Mirko Friedenhagen