java println format的区别,修改Integer的缓存大小之后,println可以输出正确答案,format不行

clover灬 2014-11-20 03:38:56
如题。
贴代码:
	
public static void setFinalStatic(Field field, Object newValue) {
try {
field.setAccessible(true);

Field modifier = Field.class.getDeclaredField("modifiers");
modifier.setAccessible(true);
modifier.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(null, newValue);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception {
Integer[] ret = new Integer[256];
java.util.Arrays.fill(ret, 42);

setFinalStatic(Class.forName("java.lang.Integer$IntegerCache").getDeclaredField("cache"), ret);

System.out.format("6 * 9 = %d", 0);

// setFinalStatic(Boolean.class.getField("FALSE"), true);
// System.out.format("Everything is %s",true);
//
// System.out.println();
// System.out.println(new Integer(0).valueOf(122220));
}
...全文
214 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
clover灬 2014-11-21
  • 打赏
  • 举报
回复

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧