Strings are objects in Java, so why don't we u

2019-01-01 02:05发布

We normally create objects using the new keyword, like:

Object obj = new Object();

Strings are objects, yet we do not use new to create them:

String str = "Hello World";

Why is this? Can I make a String with new?

14条回答
与君花间醉酒
2楼-- · 2019-01-01 02:19

It's a shortcut. It wasn't originally like that, but Java changed it.

This FAQ talks about it briefly. The Java Specification guide talks about it also. But I can't find it online.

查看更多
与君花间醉酒
3楼-- · 2019-01-01 02:24

String is subject to a couple of optimisations (for want of a better phrase). Note that String also has operator overloading (for the + operator) - unlike other objects. So it's very much a special case.

查看更多
一个人的天荒地老
4楼-- · 2019-01-01 02:27

In Java, Strings are a special case, with many rules that apply only to Strings. The double quotes causes the compiler to create a String object. Since String objects are immutable, this allows the compiler to intern multiple strings, and build a larger string pool. Two identical String constants will always have the same object reference. If you don't want this to be the case, then you can use new String(""), and that will create a String object at runtime. The intern() method used to be common, to cause dynamically created strings to be checked against the string lookup table. Once a string in interned, the object reference will point to the canonical String instance.

    String a = "foo";
    String b = "foo";
    System.out.println(a == b); // true
    String c = new String(a);
    System.out.println(a == c); // false
    c = c.intern();
    System.out.println(a == c); // true

When the classloader loads a class, all String constants are added to the String pool.

查看更多
无色无味的生活
5楼-- · 2019-01-01 02:34

You can still use new String("string"), but it would be harder to create new strings without string literals ... you would have to use character arrays or bytes :-) String literals have one additional property: all same string literals from any class point to same string instance (they are interned).

查看更多
闭嘴吧你
6楼-- · 2019-01-01 02:35

Strings are "special" objects in Java. The Java designers wisely decided that Strings are used so often that they needed their own syntax as well as a caching strategy. When you declare a string by saying:

String myString = "something";

myString is a reference to String object with a value of "something". If you later declare:

String myOtherString = "something";

Java is smart enough to work out that myString and myOtherString are the same and will store them in a global String table as the same object. It relies on the fact that you can't modify Strings to do this. This lowers the amount of memory required and can make comparisons faster.

If, instead, you write

String myOtherString = new String("something");

Java will create a brand new object for you, distinct from the myString object.

查看更多
孤独寂梦人
7楼-- · 2019-01-01 02:37

There's almost no need to new a string as the literal (the characters in quotes) is already a String object created when the host class is loaded. It is perfectly legal to invoke methods on a literal and don, the main distinction is the convenience provided by literals. It would be a major pain and waste of tine if we had to create an array of chars and fill it char by char and them doing a new String(char array).

查看更多
登录 后发表回答