java 包装类 Ingeter实例 compareTo、equals、valueof方法
java 包装类 Ingeter实例 compareTo、equals、valueof方法
定义变量,经常使用基本数据类型,对于基本数据类型来说,它就是一个数,加点属性、方法、构造器,将基本数据类型对应进行了一个封装,产生了一个新的类,—>包装类。
int,byte…—>基本数据类型
包装类—>引用数据类型
- 对应关系:
基本数据类型 | 对应的包装类 | 继承关系 |
---|---|---|
byte | Byte | *–>Number–>Object |
short | Short | *–>Number–>Object |
int | Integer | *–>Number–>Object |
long | Long | *–>Number–>Object |
float | Float | *–>Number–>Object |
double | Double | *–>Number–>Object |
boolean | Boolean | *–>Object |
char | Character | *–>Object |
- 包装类意义:
- java语言 面向对象的语言,最擅长的操作各种各样的类
- 以前学习装数据的数组,int[] String[] double[] Student[];以后学习的装数据的集合,有一个特点,只能装引用数据类型的数据
Integer实例
继承关系
- java.lang.Object
- java.lang.Number
- java.lang.Integer
public final class Integer extends Number implements Comparable<Integer>{
private final int value;
}
Integer
类在对象中包装了一个基本类型 int
的值。Integer
类型的对象包含一个 int
类型的字段。
此外,该类提供了多个方法,能在 int
类型和 String
类型之间互相转换,还提供了处理 int
类型时非常有用的其他一些常量和方法。 Integer
类被final
修饰,不能有子类,不能被继承。
Field
Modifier and Type | Field and Description |
---|---|
static int | BYTES 用于表示二进制补码二进制形式的 int 值的字节数。 |
static int | MAX_VALUE 一个持有最大值一个 int 可以有
2
31
−
1
2^{31} -1
231−1。 |
static int | MIN_VALUE 的常量保持的最小值的 int 可以具有,
−
2
31
-2^{31}
−231。 |
static int | SIZE 用于表示二进制补码二进制形式的 int 值的位数。 |
static 类<Integer> | TYPE 类 原始类型 int 的 类 实例。 |
@Native public static final int MIN_VALUE = 0x80000000;
@Native public static final int MAX_VALUE = 0x7fffffff;
@Native public static final int SIZE = 32;
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
构造方法及描述
构造方法及描述 |
---|
Integer(int value) 构造一个新分配的 Integer 对象,该对象表示指定的 int 值。 |
Integer(String s) 构造一个新分配 Integer 对象,表示 int 由指示值 String 参数。 |
public Integer(int value) {
this.value = value;
}
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}
static int parseInt(String s, int radix)
使用第二个参数指定的基数,将字符串参数解析为有符号的整数。
代码实现
public class Test01 {
public static void main(String[] args) {
Integer i1 = new Integer(8);
Integer i2 = new Integer("9");
// Integer i3 = new Integer("abc"); //Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
System.out.println(i1 + " " + i2); //8 9
}
}
自动装箱、拆箱
将基本数据类型和包装类进行快速的类型转换。
public class Test2 {
public static void main(String[] args) {
//自动装箱:int--->Integer
Integer i = 12;
System.out.println(i);
//自动拆箱:Integer--->int
Integer i2 = new Integer(12);
int num = i2;
System.out.println(num);
}
}
Ingeter 方法
Modifier and Type | Method and Description |
---|---|
static int | bitCount(int i) 返回指定的int值的二进制补码二进制表示中的 int 数。 |
byte | byteValue() 返回此值 Integer 为 byte 的基本收缩转换后。 |
static int | compare(int x, int y) 比较两个 int 数字值。 |
int | compareTo(Integer anotherInteger) 数字比较两个 Integer 对象。 |
double | doubleValue() 返回此值 Integer 为 double 一个宽元转换后。 |
boolean | equals(Object obj) 将此对象与指定的对象进行比较。 |
float | floatValue() 返回此值 Integer 为 float 一个宽元转换后。 |
static Integer | getInteger(String nm) 确定具有指定名称的系统属性的整数值。 |
static Integer | getInteger(String nm, int val) 确定具有指定名称的系统属性的整数值。 |
static Integer | getInteger(String nm, Integer val) 返回具有指定名称的系统属性的整数值。 |
int | hashCode() 返回这个 Integer 的哈希码。 |
static int | hashCode(int value) 返回值为int 的哈希码; 兼容Integer.hashCode() 。 |
int | intValue() 将 Integer 的值作为 int 。 |
long | longValue() 返回此值 Integer 为 long 一个宽元转换后。 |
static int | lowestOneBit(int i) 在指定的 int 值中,以最低位(最右边)为1位返回一个最多为单个1位的 int 值。 |
static int | max(int a, int b) 返回两个 int 的较大值,就像调用 [Math.max 一样。 |
static int | min(int a, int b) 返回两个 int 的较小值,就像调用 Math.min 一样。 |
static int | parseInt(String s) 将字符串参数解析为带符号的十进制整数。 |
static int | parseInt(String s, int radix) 将字符串参数解析为第二个参数指定的基数中的有符号整数。 |
static int | sum(int a, int b) 根据+运算符将两个整数相加。 |
static String | toBinaryString(int i) 在基数2中返回整数参数的字符串表示形式为无符号整数。 |
static String | toHexString(int i) 返回整数参数的字符串表示形式,作为16位中的无符号整数。 |
static String | toOctalString(int i) 在基数8中返回整数参数的字符串表示形式为无符号整数。 |
String | toString() 返回 String 表示此对象 Integer 的价值。 |
static String | toString(int i) 返回一个 String 指定整数的 String 对象。 |
static String | toString(int i, int radix) 返回由第二个参数指定的基数中的第一个参数的字符串表示形式。 |
static Integer | valueOf(int i) 返回一个 Integer 指定的 int 值的 Integer 实例。 |
static Integer | valueOf(String s) 返回一个 Integer 对象,保存指定的值为 String 。 |
static Integer | valueOf(String s, int radix) 返回一个 Integer 对象,保存从指定的String中 String 的值,当用第二个参数给出的基数进行解析时。 |
compareTo
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
public static void main(String[] args) {
Integer i = 12;
Integer i2 = 12;
// -1 0 1
System.out.println(i.compareTo(13)+ " "+i.compareTo(i2) + " " + i.compareTo(11));
}
equals
- Integer对Object中的
equals
方法进行了重写,比较的是底层封装的value值 - Integer对象是通过new关键字创建的对象
- 如果自动装箱值在
-128~127
之间,那么比较的就是具体的数值否则,比较的就是对象的地址
public static void main(String[] args) {
Integer i3 = new Integer(12);
Integer i4 = new Integer(12);
System.out.println(i3 == i4);//false 因为==比较的是两个对象的地址
int a = 2;
int b = 2;
System.out.println(a == b); //true
boolean flag = i3.equals(i4);
System.out.println(flag);//true
/*Integer对象通过自动装箱来完成:
如果自动装箱值在-128~127之间,那么比较的就是具体的数值
否在,比较的就是对象的地址*/
Integer i5 = 130;
Integer i6 = 130;
System.out.println(i5.equals(i6));//true
System.out.println(i5 == i6); //false
}
valueOf
如果i在 -128~127之间,从cache数组中取数,返回一个数值;否则封装为Ingeter对象。
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;
......
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() {}
}
类型转换
//intValue() :作用将Integer--->int
Integer i7 = 13;
System.out.println(i7.intValue()); //13
//parseInt(String s) :String--->int:
System.out.println(Integer.parseInt("12")); //12
//toString:Integer--->String
Integer i10 = 13;
System.out.println(i10.toString()); //13