混淆字符串中的 toString() 实现(源代码)class
confusing toString() implementation(source code) in String class
java.lang.String中toString()的重写代码如下:
public String toString(){
return this;
}
所以,打印一个字符串引用变量应该打印引用变量的地址(因为 toString() returns 'this')而不是一个字符串 literal.Why 我错了吗?
例如,考虑代码
class Sample
{
String s="dummy";
System.out.println(s);//implicit call to toString()
}
根据toString()源码中的逻辑,输出"dummy"时应该打印(变量)s的地址。为什么会这样?
查看 System.out.println
代码,它没有使用 toString
但它写入了一个从 String:
检索到的 char[]
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= WRITE_BUFFER_SIZE) {
if (writeBuffer == null) {
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
}
上述方法之前println
的调用栈(在Writer
):
PrintStream:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
作者:
public void write(String str) throws IOException {
write(str, 0, str.length());
}
toString() 应该 return a 任何对象的字符串表示(因此它存在于 java.lang.Object)。
当在字符串对象上调用时,该字符串的字符串表示是猜猜是什么...字符串本身;因此 toString() returns 是字符串本身。
首先,这段代码中没有对 toString
的隐式调用:
String s="dummy";
System.out.println(s); // <== No implicit call to toString()
s
是一个String
,所以叫System.out.println(String)
;没有 toString
发生。
之所以输出字符串的内容是因为System.out.println(String)
是为了打印字符串的内容而设计的
printing a String reference variable should print address of reference variable(because toString() returns 'this')
没有。在某些对象上调用 toString
时看到 java.lang.Object@15db9742
或类似内容的原因是 Object
return 中 toString
的实现是该形式的字符串(注意:它不是对象的地址)。但是 类 就像 String
覆盖 toString
到 return 更有用的字符串,当然在 String
的情况下,最有用的字符串 toString
可以 return 是字符串本身。
java.lang.String中toString()的重写代码如下:
public String toString(){
return this;
}
所以,打印一个字符串引用变量应该打印引用变量的地址(因为 toString() returns 'this')而不是一个字符串 literal.Why 我错了吗?
例如,考虑代码
class Sample
{
String s="dummy";
System.out.println(s);//implicit call to toString()
}
根据toString()源码中的逻辑,输出"dummy"时应该打印(变量)s的地址。为什么会这样?
查看 System.out.println
代码,它没有使用 toString
但它写入了一个从 String:
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= WRITE_BUFFER_SIZE) {
if (writeBuffer == null) {
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
}
上述方法之前println
的调用栈(在Writer
):
PrintStream:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
作者:
public void write(String str) throws IOException {
write(str, 0, str.length());
}
toString() 应该 return a 任何对象的字符串表示(因此它存在于 java.lang.Object)。
当在字符串对象上调用时,该字符串的字符串表示是猜猜是什么...字符串本身;因此 toString() returns 是字符串本身。
首先,这段代码中没有对 toString
的隐式调用:
String s="dummy";
System.out.println(s); // <== No implicit call to toString()
s
是一个String
,所以叫System.out.println(String)
;没有 toString
发生。
之所以输出字符串的内容是因为System.out.println(String)
是为了打印字符串的内容而设计的
printing a String reference variable should print address of reference variable(because toString() returns 'this')
没有。在某些对象上调用 toString
时看到 java.lang.Object@15db9742
或类似内容的原因是 Object
return 中 toString
的实现是该形式的字符串(注意:它不是对象的地址)。但是 类 就像 String
覆盖 toString
到 return 更有用的字符串,当然在 String
的情况下,最有用的字符串 toString
可以 return 是字符串本身。