package java.lang;
public abstract class Number implements java.io.Serializable {
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
private static final long serialVersionUID = -8742448824652078965L;
}
public class Test {
public static void main(String[] args) {
// 手动装箱(过时)
Integer x = new Integer(3);
// 手动拆箱(过时)
int y = x.intValue();
System.out.println(y); // 3
// 自动装箱
Integer a = 4;
// 自动拆箱
int b = a;
System.out.println(b); // 4
}
}
public class Test {
public static void main(String[] args) {
/**
* 编译正常
* 运行异常:java.lang.NumberFormatException
*/
Integer i = new Integer("创建日期");
System.out.println(i);
}
}
public class Test04 {
public static void main(String[] args) {
/**
* static int parseInt(String s)
* 静态方法,传参String,返回int
*/
String str = "123";
int i = Integer.parseInt(str);
System.out.println(i); // 输出int类型的123
}
}
public class Test {
public static void main(String[] args) {
/**
* 自动装箱的结果比较(由valueOf方法实现)
* Integer采用了缓存机制
* Double没有采用缓存机制
*/
Integer i1 = -128;
Integer i2 = -128;
Integer i3 = 100;
Integer i4 = 100;
Integer i5 = 127;
Integer i6 = 127;
Double d1 = 10.0;
Double d2 = 10.0;
Double d3 = 200.0;
Double d4 = 200.0;
System.out.println(i1 == i2); // true
System.out.println(i3 == i4); // true
System.out.println(i5 == i6); // false
System.out.println(d1 == d2); // false
System.out.println(d3 == d4); // false
}
}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)]; // 符合取值范围时,进入已创建好的静态IntergerCache中(预加载的缓存),i + (-IntegerCache.low)的值表示去取cache数组中那个下标的值
return new Integer(i); // 当不符合取值范围时,创建新的对象,即new开辟新的内存空间,此时不属于IntergerCache管理区
}
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 int intValue() {
return value; // 直接返回拆箱后的结果
}
public static Boolean valueOf(boolean b) {
/**
* TRUE与FALSE是一个对象
* 声明方式:
* public static final Boolean TRUE = new Boolean(true);
* public static final Boolean FALSE = new Boolean(false);
* 因为只有两种情况,所以在内部已经提前创建好两个对象,为了避免重复创建对象
*/
return (b ? TRUE : FALSE);
}
public static Double valueOf(double d) {
/**
* 在某个范围内,浮点数的个数是无限的,所以Double没有采用缓存机制
* Double直接创建一个对象,所以每次创建的对象都不一样
*/
return new Double(d);
}
public class Test {
public static void main(String[] args) {
/**
* 两个Integer对象相加时,编译器会将Integer拆箱为int相加
* 即所得和为int类型,此时也无法比较,所以将前者同理拆箱为int类型
*/
Integer i1 = new Integer(100);
Integer i2 = new Integer(100);
Integer i3 = new Integer(0);
System.out.println(i1 == i2); // false
System.out.println(i1 == i2 + i3); // true
}
}
public class Test {
public static void main(String[] args) {
// String --> int
int i1 = Integer.parseInt("100"); // i1是数字100
System.out.println(i1 + 1); // 101
// int --> String
String s2 = i1 + "";
System.out.println(s2); // "100"
// int --> Integer
// 自动装箱
Integer x = 1000;
// Integer --> int
// 自动拆箱
int y = x;
// String --> Integer
Integer k = Integer.valueOf("123");
// Integer --> String
String e = String.valueOf(k);
}
}
本文地址:https://blog.csdn.net/LvJzzZ/article/details/107579583
如对本文有疑问, 点击进行留言回复!!
Springboot项目因为kackson版本问题启动报错解决方案
Java多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask详解
网友评论