Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single character String

Tags:

java

My opinion was, until today, that a literal like "c" creates a String object. Today I heard that Java is not creating an object for single character strings. Is this right? Does it store that literal as a char?

like image 815
Tchypp Avatar asked Jun 24 '11 12:06

Tchypp


People also ask

What is a one character string?

A character string differs from a name in that it does not represent anything — a name stands for some other object. A character string is often specified by enclosing the characters in single or double quotes. For example, WASHINGTON would be a name, but 'WASHINGTON' and “WASHINGTON” would be character strings.

Can a string hold a single character?

A String containing a single character is not the same as a char. They both can be declared using quotes (single quotes for chars and double quotes for Strings), but they are very different. At a high level, a way to think about it is that a String is an Object that allows you to operate on a sequence of chars.

Can a string be one letter?

You can certainly make a string equal a single character if you wish, it takes more overheard from resources but, it's certainly do able, this would unlock many methods on that string as well.


6 Answers

No it's wrong. Even "", creates a String object. However if you type 'c', you got a char and not a String object.

like image 79
Giann Avatar answered Oct 04 '22 06:10

Giann


"c" will create a string. 'c' will create a char

like image 25
Tom Squires Avatar answered Oct 04 '22 04:10

Tom Squires


Java does create an instance of a string even for a single character string. The following prints java.lang.String:

public class Test{
    public static void main(final String[] args){
        System.out.println("c".getClass().getName());
    }
}
like image 27
Jack Edmonds Avatar answered Oct 04 '22 06:10

Jack Edmonds


"c" is a String literal. It represents a String just as "foo" represents a String.

There is no special handling of single-character String literals (not even of the 0-letter String literal "").

Whoever told you that it's treated differently was either a.) wrong or b.) talking about something different (a library that has special treatment, for example).

like image 41
Joachim Sauer Avatar answered Oct 04 '22 05:10

Joachim Sauer


"c" does create an object. However, if you assign the literal again in somewhere in the source code, it will not create a new object, but reference the first string object created.

For example:

String s1 = "abc";   //creates the String object
String s2 = "abc";   //references the same object as s1

Both s1 and s2 are assigned the same object, and == would work.

You can read more here: http://javatechniques.com/blog/string-equality-and-interning/

like image 41
xkrz Avatar answered Oct 04 '22 06:10

xkrz


Maybe what was meant was that the beneath the hood flyweights are created (dunno how this works with Java, but I presume that this concept is employeed at some level for strings)

like image 40
Denis Biondic Avatar answered Oct 04 '22 06:10

Denis Biondic