关于java中String类的一个疑义
关于java中String类的一个疑问
java的String类中字符串的存储方式为char型的数组:private final char value[];
但是当输出这个字符串时如System.out.println(new String("abc"));
输出的结果是abc.
String类的哪一段代码控制了这样的输出呢?还是虚拟机内部的某些东西在控制着?
求解答。。。。。。
------解决思路----------------------
输出对象时,自动调用其toString方法,如果没有复写,会调用根类OBject的该方法,该方法的输出格式就这样,自己看下源码就知道
------解决思路----------------------
这种问题最好的方式就是看jdk源码。看看下面这段源码你就知道输出String的时候是怎么输出的了。
这是Writer类的write方法的一段
------解决思路----------------------
怎么找到还是查源码
返回对象没错
但是怎么完成输出怎么找到字符串不是String完成的
而是由输出函数println完成的
查看源码
注: ----->代表调用
println(String)---->print(String)---->write(String)---> BufferWriter的textOut.write(String)即BufferWriter父类Writer的write(String)---> write(str, 0, str.length())---->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);
}
}---->abstract public void write(char cbuf[], int off, int len),即调用子类的
public void write(char cbuf[], int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
if ((off < 0)
------解决思路----------------------
(off > cbuf.length)
------解决思路----------------------
(len < 0)
------解决思路----------------------
((off + len) > cbuf.length)
------解决思路----------------------
((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
if (len >= nChars) {
/* If the request length exceeds the size of the output buffer,
flush the buffer and then write the data directly. In this
way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(cbuf, off, len);
return;
}
int b = off, t = off + len;
while (b < t) {
int d = min(nChars - nextChar, t - b);
System.arraycopy(cbuf, b, cb, nextChar, d);
b += d;
nextChar += d;
if (nextChar >= nChars)
flushBuffer();
}
}
}
终于绕完了,java的设计分工之细不得不佩服
其实是调用一些列函数完成的,我们只是用到了最上面最直接的功能而已
个人的感觉是print(Int)和print(String)一样简单舒服
但是具体实现却不一样
java的String类中字符串的存储方式为char型的数组:private final char value[];
但是当输出这个字符串时如System.out.println(new String("abc"));
输出的结果是abc.
String类的哪一段代码控制了这样的输出呢?还是虚拟机内部的某些东西在控制着?
求解答。。。。。。
------解决思路----------------------
输出对象时,自动调用其toString方法,如果没有复写,会调用根类OBject的该方法,该方法的输出格式就这样,自己看下源码就知道
------解决思路----------------------
这种问题最好的方式就是看jdk源码。看看下面这段源码你就知道输出String的时候是怎么输出的了。
这是Writer类的write方法的一段
/**
* Writes a string.
*
* @param str
* String to be written
*
* @throws IOException
* If an I/O error occurs
*/
public void write(String str) throws IOException {
write(str, 0, str.length());
}
/**
* Writes a portion of a string.
*
* @param str
* A String
*
* @param off
* Offset from which to start writing characters
*
* @param len
* Number of characters to write
*
* @throws IndexOutOfBoundsException
* If <tt>off</tt> is negative, or <tt>len</tt> is negative,
* or <tt>off+len</tt> is negative or greater than the length
* of the given string
*
* @throws IOException
* If an I/O error occurs
*/
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= writeBufferSize) {
if (writeBuffer == null) {
writeBuffer = new char[writeBufferSize];
}
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);
}
}
------解决思路----------------------
怎么找到还是查源码
返回对象没错
但是怎么完成输出怎么找到字符串不是String完成的
而是由输出函数println完成的
查看源码
注: ----->代表调用
println(String)---->print(String)---->write(String)---> BufferWriter的textOut.write(String)即BufferWriter父类Writer的write(String)---> write(str, 0, str.length())---->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);
}
}---->abstract public void write(char cbuf[], int off, int len),即调用子类的
public void write(char cbuf[], int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
if ((off < 0)
------解决思路----------------------
(off > cbuf.length)
------解决思路----------------------
(len < 0)
------解决思路----------------------
((off + len) > cbuf.length)
------解决思路----------------------
((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
if (len >= nChars) {
/* If the request length exceeds the size of the output buffer,
flush the buffer and then write the data directly. In this
way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(cbuf, off, len);
return;
}
int b = off, t = off + len;
while (b < t) {
int d = min(nChars - nextChar, t - b);
System.arraycopy(cbuf, b, cb, nextChar, d);
b += d;
nextChar += d;
if (nextChar >= nChars)
flushBuffer();
}
}
}
终于绕完了,java的设计分工之细不得不佩服
其实是调用一些列函数完成的,我们只是用到了最上面最直接的功能而已
个人的感觉是print(Int)和print(String)一样简单舒服
但是具体实现却不一样