Integer类
本文将介绍Integer类,Integer类是基本数据类型int的包装类,继承了Number类,并且实现了Comparable接口。
1public final class Integer extends Number implements Comparable<Integer>{}
构造方法
1
2
3
4
5
6
7
8
9
10//构造一个新分配的 Integer 对象,它表示指定的 int 值 public Integer(int value) { this.value = value; } //构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。 public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); }
也就是说,创建对象时传入一个数字或者字符形式的参数,都将会赋予value一个int型的值。
常用方法
成员函数
1.compareTo方法
方法描述:在数字上比较两个 Integer
对象。
返回值:如果该 Integer
等于 Integer
参数,则返回 0
值;如果该 Integer
在数字上小于 Integer
参数,则返回小于 0
的值;如果 Integer
在数字上大于 Integer
参数,则返回大于 0
的值(有符号的比较)。
1
2
3
4
5
6
7
8public int compareTo(Integer anotherInteger) {//传入一个Integer对象 return compare(this.value, anotherInteger.value);//调用该类中的compare方法返回比较值 } //----------------------以下是compare方法---------------------- public static int compare(int x, int y) {//传入两个int型数值 //判断是否x<y,若true返回-1,反之判断是否x=y,若true返回0,反之返回1. return (x < y) ? -1 : ((x == y) ? 0 : 1); }
2.intValue方法
方法描述:以 int
类型返回该 Integer
的值。
返回值:转换为 int
类型后该对象表示的数值。
1
2
3
4public int intValue() { return value;//直接返回该对象中的int值 }
3.toString方法
方法描述:返回一个表示该 Integer
值的 String
对象。
返回值:该对象的值(基数 10)的字符串表示形式。
1
2
3
4
5
6
7
8
9
10
11
12public String toString() { return toString(value); } //—-------------以下是重载方法toString(int i);------------ public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-2147483648"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); char[] buf = new char[size]; getChars(i, size, buf); return new String(buf, true);//返回一个字符串对象表现形式的int数据 }
静态函数
1.parseInt方法
方法描述:将字符串参数作为一个符号整数,进制由第二个参数指定。
返回值:使用指定基数的字符串参数表示的整数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58//静态方法使用第二个参数指定的基数(进制),将字符串参数解析为有符号的整数除了第一个字符可以是用来表示负值的 ASCII 减号 '-' ('u002D’),加号'+' ('u002B') 外字符串中的字符必须都是指定基数的数字 public static int parseInt(String s, int radix) throws NumberFormatException { if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0; boolean negative = false; int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; int multmin; int digit; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; } //静态方法static int parseInt(String s, int radix) 的十进制简化形式 public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }
2.revers方法
方法描述:返回通过反转指定 int 值的二进制补码表示形式中位的顺序而获得的值。
返回值:返回通过反转指定 int 值中位的顺序而获得的值。
1
2
3
4
5
6
7
8
9public static int reverse(int i) { // HD, Figure 7-1 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; i = (i << 24) | ((i & 0xff00) << 8) | ((i >>> 8) & 0xff00) | (i >>> 24); return i; }
3.toBinaryString方法
方法描述:以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
返回值:用二进制(基数 2)参数表示的无符号整数值的字符串表示形式。
1
2
3
4
5
6
7
8
9
10
11
12
13public static String toBinaryString(int i) { return toUnsignedString0(i, 1); } //+++++++++++++++++++++++++++++++++++++++++++++++ private static String toUnsignedString0(int val, int shift) { // assert shift > 0 && shift <=5 : "Illegal shift value"; int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); int chars = Math.max(((mag + (shift - 1)) / shift), 1); char[] buf = new char[chars]; formatUnsignedInt(val, shift, buf, 0, chars); // Use special constructor which takes over "buf". return new String(buf, true); }
4.toOctalString方法
方法描述:以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
返回值:用八进制参数(基数 8)表示的无符号整数值的字符串表示形式。
1
2
3
4
5
6
7
8
9
10
11
12
13public static String toOctalString(int i) { return toUnsignedString0(i, 3); } //+++++++++++++++++++++++++++++++++++++++++++++++ private static String toUnsignedString0(int val, int shift) { // assert shift > 0 && shift <=5 : "Illegal shift value"; int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); int chars = Math.max(((mag + (shift - 1)) / shift), 1); char[] buf = new char[chars]; formatUnsignedInt(val, shift, buf, 0, chars); // Use special constructor which takes over "buf". return new String(buf, true); }
5.toHexString(int)方法
方法描述:以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
返回值:参数的十六进制(基数 16)无符号整数值的字符串表示形式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14public static String toHexString(int i) { return toUnsignedString0(i, 4); } //+++++++++++++++++++++++++++++++++++++++++++++++ private static String toUnsignedString0(int val, int shift) { // assert shift > 0 && shift <=5 : "Illegal shift value"; int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); int chars = Math.max(((mag + (shift - 1)) / shift), 1); char[] buf = new char[chars]; formatUnsignedInt(val, shift, buf, 0, chars); // Use special constructor which takes over "buf". return new String(buf, true); }
6.toString(int)方法
方法描述:返回用第二个参数指定基数表示的第一个参数的字符串表示形式。
返回值:使用指定基数的参数的字符串表示形式。
1
2
3
4
5
6
7
8
9public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-2147483648"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); char[] buf = new char[size]; getChars(i, size, buf); return new String(buf, true); }
7.valueOf方法
方法描述:返回保存指定的 String
的值的 Integer
对象。
返回值:保存字符串参数表示的值的 Integer
对象。
1
2
3
4
5
6
7
8
9public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
下面是几种Integer常用方法例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32public class NumberDemo01 { public static void main(String[] args) { Integer i1=new Integer(10); //10 Integer i2=new Integer("10"); //10 //成员函数 System.out.println(i1.compareTo(i2));//0 System.out.println(i1<i2); //【注意!】false System.out.println(i1+i2);//20 System.out.println(i1.equals(i2));//true System.out.println(i1==i2);//false 两个对象的地址不同 System.out.println(i1.intValue()==i2.intValue());//true System.out.println(i1.toString());//将包装类型->字符串 等效于 10+"" //静态函数 System.out.println(Integer.parseInt("123")+1); //默认以十进制解析字符串 System.out.println(Integer.parseInt("10010101",2)); //以二进制解析字符串 //java.lang.NumberFormatException //System.out.println(Integer.parseInt("10010102",2)); //以二进制解析字符串 System.out.println(Integer.parseInt("102201012",3));//以三进制解析字符串 //最高支持几进制?radix ∈ [2,36] 10个数字+26个字母 System.out.println(Integer.parseInt("123123",36)); System.out.println(Integer.reverse(123));//不是我们简单认为的123-321 System.out.println(Integer.toBinaryString(149)); System.out.println(Integer.toOctalString(149)); System.out.println(Integer.toHexString(149)); System.out.println(Integer.toString(149)); //不是Object重写来的 //静态工厂模式 System.out.println(Integer.valueOf(10)); //用方法的形式创建一个对象,等效于 new Integer(10) } }
最后
以上就是奋斗牛排最近收集整理的关于Integer类——常用方法及应用举例Integer类的全部内容,更多相关Integer类——常用方法及应用举例Integer类内容请搜索靠谱客的其他文章。
发表评论 取消回复