我是靠谱客的博主 大意宝马,这篇文章主要介绍基本类型包装类的引入-Integer基本类型包装类的引入 Integer的构造方法:int类和String类型的相互转换常用的基本进制转换: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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50package wrapper_class; /* * 基本类型包装类的引入 * * 需求1: 把100这个数据的二进制,八进制,十六进制计算出来 * 需求2:判断一个数据是否是int范围内的。 * 首先得知道int的范围是多大? * * 为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型。包装类类型。 * byte Byte * short Short * int Integer * long Long * floa Floa * double Double * char Character * boolean Boolean */ public class IntegerDemo { public static void main(String[] args) { // public static String toBinaryString(int i) System.out.println(Integer.toBinaryString(100)); // public static String toOctalString(int i) System.out.println(Integer.toOctalString(100)); // public static String toHexString(int i) System.out.println(Integer.toHexString(100)); // public static final int MAX_VALUE System.out.println(Integer.MAX_VALUE); // public static final int MIN_VALUE System.out.println(Integer.MIN_VALUE); } }
Integer的构造方法:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package wrapper_class; /* * Integer的构造方法: * public Integer(int value) * public Integer(String s) * * 注意:这个字符串必须是由数字字符组成的 */ public class IntegerDemo2 { public static void main(String[] args) { // 方式1 int i = 100; Integer ii = new Integer(i); System.out.println("ii:" + ii); // 方式2 String s = "100"; Integer iii = new Integer(s); System.out.println("iii:" + iii); } }
int类和String类型的相互转换
复制代码
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
46package wrapper_class; /* * int类和String类型的相互转换 * * int -- String * 推荐:String.valueOf(number) * String -- int * 推荐:Integer.parseInt(s) */ public class IntegerDemo3 { public static void main(String[] args) { // int -- String int number = 100; // 方式1 String s1 = "" + number; System.out.println("s1:" + s1); // 方式2 String s2 = String.valueOf(number); System.out.println("s2:" + s2); // 方式3 // int -- Integer -- String Integer i = new Integer(number); String s3 = i.toString(); System.out.println("s3:" + s3); // 方式4 // public static String toString(int i) String s4 = Integer.toString(number); System.out.println("s4:" + s4); System.out.println("--------------"); // String -- int String s = "100"; // 方式1 // String -- Integer -- Int Integer ii = new Integer(s); // public int intValue() int x = ii.intValue(); System.out.println("x:" + x); // 方式2 // public static int parseInt(String s) int xx = Integer.parseInt(s); System.out.println("xx:" + xx); } }
常用的基本进制转换:
复制代码
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
57package wrapper_class; /* * 常用的基本进制转换: * public static String toBinaryString(int i) * public static String toOctalString(int i) * public static String toHexString(int i) * 十进制到其它进制: * public static String toString(int i,int radix) * 由于测试出了进制的范围:2-36 * 为什么呢?0,...9,a,...z * * 其它进制到十进制: * public static int parseInt(String s,int radix) */ public class IntegerDemo4 { public static void main(String[] args) { // 十进制到二进制、八进制、十六进制 printHex(100); System.out.println("------------------"); // 十进制到其它进制 // xxx的xx进制是xxx System.out.println(Integer.toString(100, 10)); System.out.println(Integer.toString(100, 2)); System.out.println(Integer.toString(100, 8)); System.out.println(Integer.toString(100, 16)); System.out.println(Integer.toString(100, 5)); System.out.println(Integer.toString(100, 7)); System.out.println(Integer.toString(100, -7)); System.out.println(Integer.toString(100, 70)); System.out.println(Integer.toString(100, 0)); System.out.println(Integer.toString(100, 32)); System.out.println(Integer.toString(100, 37)); System.out.println(Integer.toString(100, 36)); System.out.println("-----------------------"); // 其它进制到十进制 // xx进制的xx是xx System.out.println(Integer.parseInt("100", 10)); System.out.println(Integer.parseInt("100", 2)); System.out.println(Integer.parseInt("100", 8)); System.out.println(Integer.parseInt("100", 16)); System.out.println(Integer.parseInt("100", 23)); // NumberFormatException // System.out.println(Integer.parseInt("123", 2)); } // 常用的基本进制转换 public static void printHex(int i) { System.out.println(i + "的十进制到二进制:" + Integer.toBinaryString(i)); System.out.println(i + "的十进制到八进制:" + Integer.toOctalString(i)); System.out.println(i + "的十进制到十六进制:" + Integer.toHexString(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
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
59
60package wrapper_class; /* * Integer直接赋值的面试题 * * 看程序写结果 * * 注意:Integer的数据直接赋值,如果在-127到128之间,会直接从缓冲池里获取数据。 */ public class IntegerDemo6 { public static void main(String[] args) { Integer i1 = new Integer(127);// false,大了直接new了一个构造方法 Integer i2 = new Integer(127); System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println("-----------"); Integer i3 = new Integer(128);// false,大了直接new了一个构造方法 Integer i4 = new Integer(128); System.out.println(i3 == i4); System.out.println(i3.equals(i4)); System.out.println("-----------"); Integer i5 = 128; Integer i6 = 128; System.out.println(i5 == i6);// false,大了直接返回new了一个构造方法 System.out.println(i5.equals(i6)); System.out.println("-----------"); Integer i7 = 127; Integer i8 = 127; System.out.println(i7 == i8);// 缓冲池里找,有就true,没有false System.out.println(i7.equals(i8)); System.out.println("-----------"); Integer i9 = 126; Integer i10 = 126; System.out.println(i9 == i10); System.out.println(i9.equals(i10)); System.out.println("-----------"); // 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间 // Integer ii = Integer.valueOf(127); // public static Integer valueOf(int i) { // final int offset = 128; // if (i >= -128 && i <= 127) { //127>=-128 && 127<=127, 126>=128 && // 126<=127 // return IntegerCache.cache[i + offset];//127+128=255, 126+128=254 // } // return new Integer(i); // } // private static class IntegerCache { // private IntegerCache(){} // // static final Integer cache[] = new Integer[-(-128) + 127 + // 1];//0索引是255, 0索引是254 // // static { // for(int i = 0; i < cache.length; i++)//0 // cache[i] = new Integer(i - 128);//255 = new Integer(i-128), 254 = new // Integer(i - 128) // } // } } }
最后
以上就是大意宝马最近收集整理的关于基本类型包装类的引入-Integer基本类型包装类的引入 Integer的构造方法:int类和String类型的相互转换常用的基本进制转换:Integer直接赋值的面试题的全部内容,更多相关基本类型包装类内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复