gpt4 book ai didi

java - 字符串类与其他类有何区别?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:05 25 4
gpt4 key购买 nike

我们可以做到:

String string = "ourstring";

但是我们不能为用户定义的类创建这样的对象:

用户类 uc="";

Java 如何允许我们仅将值直接设置到 java.lang.String 类?

最佳答案

java.lang.String 是一个特殊的类。

欢迎阅读 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

它说

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.

Java 语言没有其他类有这种特殊支持。

你应该非常小心它的 + 特性:它被广泛讨论为对性能和内存不安全。

关于java - 字符串类与其他类有何区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16088255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com