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

clover灬 clover灬 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题。
贴代码:
	
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));
}
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
clover灬
Java 51,407 成员 86,129 内容