iberck
iberck

Reputation: 2430

Difference between '.' and "." in java

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";

Upvotes: 8

Views: 9944

Answers (11)

Otávio Décio
Otávio Décio

Reputation: 74270

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

Upvotes: 10

Aman Sharma
Aman Sharma

Reputation: 39

you can only put a single char in between '' if you want to add some more characters use those in between "".

as string is a collection of characters like strong text"hello world"

we can have same thing by using '' like- 'h' 'e' 'l' 'l' 'o' ..... and each has to be stored in different char variable which can be very hectic so use "" when you have more than one char to store and '' for single char

Upvotes: 0

Ben Hardy
Ben Hardy

Reputation: 1759

You will probably find the following articles useful:

Upvotes: 1

John
John

Reputation: 73

char theLetterA = 'A'; string myString = "a string";

Upvotes: 0

Mirko Friedenhagen
Mirko Friedenhagen

Reputation: 403

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.

Upvotes: 4

John Nilsson
John Nilsson

Reputation: 17307

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.

Upvotes: 14

Peter Lawrey
Peter Lawrey

Reputation: 533520

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.

Upvotes: 2

OscarRyz
OscarRyz

Reputation: 199225

'' 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 

Upvotes: 3

Fortyrunner
Fortyrunner

Reputation: 12782

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.

Upvotes: 1

Darron
Darron

Reputation: 21628

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

Once you concatenate them together there is no difference.

Upvotes: 3

Rob
Rob

Reputation: 48369

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.

Upvotes: 27

Related Questions