Java 为什么AbstractStringBuilder.append的最小值行为不同?
考虑Java 为什么AbstractStringBuilder.append的最小值行为不同?,java,Java,考虑java.lang.AbstractStringBuilder Long public AbstractStringBuilder append(long l) { if (l == Long.MIN_VALUE) { append("-9223372036854775808"); return this; } int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
java.lang.AbstractStringBuilder
Long
public AbstractStringBuilder append(long l) {
if (l == Long.MIN_VALUE) {
append("-9223372036854775808");
return this;
}
int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
: Long.stringSize(l);
int spaceNeeded = count + appendedLength;
ensureCapacityInternal(spaceNeeded);
Long.getChars(l, spaceNeeded, value);
count = spaceNeeded;
return this;
}
public AbstractStringBuilder追加(长l){
如果(l==长.MIN_值){
追加(“-9223372036854775808”);
归还这个;
}
int appendedLength=(l<0)?Long.stringSize(-l)+1
:长尺寸(l);
int spaceNeeded=计数+追加长度;
确保重新产能内部(所需空间);
Long.getChars(l,spaceNeeded,value);
计数=所需空间;
归还这个;
}
整数
public AbstractStringBuilder append(int i) {
if (i == Integer.MIN_VALUE) {
append("-2147483648");
return this;
}
int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
: Integer.stringSize(i);
int spaceNeeded = count + appendedLength;
ensureCapacityInternal(spaceNeeded);
Integer.getChars(i, spaceNeeded, value);
count = spaceNeeded;
return this;
}
public AbstractStringBuilder append(inti){
if(i==Integer.MIN_值){
追加(“-2147483648”);
归还这个;
}
int appendedLength=(i<0)?整数.stringSize(-i)+1
:整数。字符串大小(i);
int spaceNeeded=计数+追加长度;
确保重新产能内部(所需空间);
getChars(i,spaceNeeded,value);
计数=所需空间;
归还这个;
}
为什么
AbstractStringBuilder#append
使用不同的算法来追加MIN_值
?因为stringSize
算法根据输入的绝对值估计所需的字符数,除了MIN\u VALUE
没有可表示的绝对值外:-Integer.MIN\u VALUE==Integer.MIN\u VALUE
因为stringSize
算法根据输入的绝对值估计所需的字符数,除了MIN\u VALUE
没有可表示的绝对值外:-Integer.MIN\u VALUE==Integer.MIN\u VALUE
,因为Integer.stringSize
需要非负参数。代码如下所示:
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}
final static int[]sizeTable={9,99,999,9999,99999,9999999,
999999999,999999999,Integer.MAX_VALUE};
//需要正x
静态整数字符串大小(整数x){
对于(int i=0;i++)
if(x因为Integer.stringSize
需要一个非负参数。代码如下所示:
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}
final static int[]sizeTable={9,99,999,9999,99999,9999999,
999999999,999999999,Integer.MAX_VALUE};
//需要正x
静态整数字符串大小(整数x){
对于(int i=0;i++)
如果(x)你的意思是为什么两个MIN\u值都是特殊情况?你的意思是为什么两个MIN\u值都是特殊情况?