public static void test() throws Exception {
Integer a = 1, b = 2;
swap(a, b);
System.out.println("a=" + a + ", b=" + b);
}
static void swap(Integer a, Integer b){
// 需要实现的部分
}
public static void swapOne(Integer a, Integer b) throws Exception {
Integer aTempValue = a;
a = b;
b = aTempValue;
}
public static void swapTwo(Integer a1, Integer b1) throws Exception {
Field valueField = Integer.class.getDeclaredField("value");
valueField.setAccessible(true);
int tempAValue = valueField.getInt(a1);
valueField.setInt(a1, b1.intValue());
valueField.setInt(b1, tempAValue);
}
public static void swapTwo(Integer a1, Integer b1) throws Exception {
Field valueField = Integer.class.getDeclaredField("value");
valueField.setAccessible(true);
int tempAValue = valueField.getInt(a1);
valueField.setInt(a1, b1.intValue());
valueField.setInt(b1, tempAValue);
}
public static void testThree() throws Exception {
Integer a = 1, b = 2;
swapTwo(a, b);
System.out.println("a=" + a + "; b=" + b);
Integer c = 1, d = 2;
System.out.println("c=" + c + "; d=" + d);
}
a=2; b=1 c=2; d=1
public static void testThree() throws Exception {
Integer a = Integer.valueOf(1);
Integer b = Integer.valueOf(2);
swapTwo(a, b);
System.out.println("a=" + a + "; b=" + b);
Integer c = Integer.valueOf(1);
Integer d = Integer.valueOf(2);
System.out.println("c=" + c + "; d=" + d);
}
public class Integer{
/**
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
}
public static void testOne() throws Exception {
int a = 1, b = 2;
swapOne(a, b);
System.out.println("a=" + a + ", b=" + b);
}
static void swapOne(int a, int b){
// 需要实现的部分
}
public static void testOne() {
int one = 1;
int two = one + one;
System.out.printf("Two=%d", two);
}
public static void testOne() {
int one = 1;
int two = one + one;
System.out.printf("Two=%d", two);
}
LDC "Two=%d" ICONST_1 ANEWARRAY java/lang/Object DUP ICONST_0 ILOAD 2 INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; AASTORE INVOKEVIRTUAL java/io/PrintStream.printf (Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream; POP
public static void testOne() {
int one = 1;
int two = one + one;
Object[] params = { Integer.valueOf(two) };
System.out.printf("Two=%d", params);
}
public class OnePlusOne {
static {
try {
Class<?> cacheClazz = Class.forName("java.lang.Integer$IntegerCache");
Field cacheField = cacheClazz.getDeclaredField("cache");
cacheField.setAccessible(true);
Integer[] cache = (Integer[]) cacheField.get(null);
//这里修改为 1 + 1 = 3
cache[2 + 128] = new Integer(3);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testOne() {
int one = 1;
int two = one + one;
System.out.printf("Two=%d", two);
}
}
public static void testTwo() {
int one = 1;
int two = one + one;
System.out.println(two == 2);
System.out.println(Integer.valueOf(two) == 2);
}
true false
| 类 | 是否有Cache | 最小值 | 最大值 |
|---|---|---|---|
| Boolean | 无 | -- | -- |
| Byte | ByteCache | -128 | 127(固定) |
| Short | ShortCache | -128 | 127(固定) |
| Character | CharacterCache | 0 | 127(固定) |
| Integer | IntegerCache | -128 | java.lang.Integer.IntegerCache.high |
| Long | LongCache | -128 | 127(固定) |
| Float | 无 | -- | -- |
| Double | 无 | -- | -- |
public static void main(String[] args) {
Integer a1 = Integer.valueOf(60); //danielinbiti
Integer b1 = 60;
System.out.println("1:="+(a1 == b1));
Integer a2 = 60;
Integer b2 = 60;
System.out.println("2:="+(a2 == b2));
Integer a3 = new Integer(60);
Integer b3 = 60;
System.out.println("3:="+(a3 == b3));
Integer a4 = 129;
Integer b4 = 129;
System.out.println("4:="+(a4 == b4));
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有