How string class difference from other classes?

后端 未结 4 677
不思量自难忘°
不思量自难忘° 2021-01-21 07:37

We can do:

String string = \"ourstring\";

But we can\'t create objects like this for user defined classes:

UserClass uc=\"\";

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-21 08:35

    java.lang.String is a special class.

    Feel free to read http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

    It says

    The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

    ...

    The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

    No other classes have this special support from Java language.

    You should be very careful with its + feature: it is widely discussed as not safe for performance and memory.

提交回复
热议问题