java基础总结20-java常用API(基本类型包装类)

来源:互联网 发布:c语言中的延时函数 编辑:程序博客网 时间:2024/05/17 07:07

1 基本类型包装类

为了方便操作基本数据类型值,将基本数据类型封装成对象,在对象中定义了属性和行为,用于描述该对象的类就称为基本数据类型对象包装类,好处在于可以在对象中定义更多的功能方法操作该数据。常用的操作之一:用于基本数据类型与字符串之间的转换。

这里写图片描述

1.1 Integer

Integer类概述:Integer 类在对象中包装了一个基本类型 int 的值

该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法

构造方法

  • public Integer(int value):

构造一个新分配的 Integer 对象,它表示指定的 int 值。

  • public Integer(String s)throws NumberFormatException:

构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。

例:

int i = 100;Integer i1 = new Integer(i);System.out.println("Integer:"+i1);String s = "100";Integer i2 = new Integer(s);System.out.println("Integer:"+i2);

String和int类型的相互转换

int→String

1.使用字符串的拼接 ” “+number

2.使用方法 String.valueOf(int number)(推荐使用)

3.使用Integer做桥梁,int→Integer→String

Integer i = new Integer(number);

String s = i.toString();

4.使用方法 Integer.toString(int number)

String→int

1.使用Integer做桥梁,String→Integer→int

Integer i = new Integer(s);

int ii = i.intValue();

2.使用方法 Integer.parseInt(String s)(推荐使用)

JDK5的新特性自动装箱和拆箱

自动装箱:把基本类型转换为包装类类型

自动拆箱:把包装类类型转换为基本类型

JDK1.5以后,简化了定义方式。

Integer x = new Integer(4);可以直接写成

Integer x = 4;//相当于Integer x = Integer.valueOf(4);自动装箱

x = x + 5;//相当于x = Integer.valueOf(x.intValue() + 5);先自动拆箱,再自动装箱

需要注意:在使用时,Integer x = null;上面的代码就会出现NullPointerException。

Integer直接赋值的面试题

  • Integer i = 1;i += 1;做了哪些事情

编译时:

Integer i = 1;相当于Integer i = Integer.valueOf(1);

i += 1;相当于i = Integer.valueOf(i.intValue() + 1);

  • 看程序写结果
Integer i1 = new Integer(127);Integer i2 = new Integer(127);System.out.println(i1 == i2);//falseSystem.out.println(i1.equals(i2));//trueInteger i3 = new Integer(128);Integer i4 = new Integer(128);System.out.println(i3 == i4);//falseSystem.out.println(i3.equals(i4));//trueInteger i5 = 127;Integer i6 = 127;System.out.println(i5 == i6);//trueSystem.out.println(i5.equals(i6));//trueInteger i7 = 128;Integer i8 = 128;System.out.println(i7 == i8);//falseSystem.out.println(i7.equals(i8));//true

注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果,数据是该范围内的,每次并不创建新的空间

1.2 Character

Character 类在对象中包装一个基本类型 char 的值。Character 类型的对象包含类型为 char 的单个字段。

此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然。

构造方法

public Character(char value):

构造一个新分配的 Character 对象,用以表示指定的 char 值。

Character的常见方法讲解

  • public static boolean isUpperCase(char ch):

确定指定字符是否为大写字母。

  • public static boolean isLowerCase(char ch):

确定指定字符是否为小写字母。

  • public static boolean isDigit(char ch):

确定指定字符是否为数字。

  • public static char toUpperCase(char ch):

使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为大写。

  • public static char toLowerCase(char ch):

使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为小写。

1.3 Math

Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

成员方法:

  • public static int abs(int a):

返回 int 值的绝对值。

  • public static double ceil(double a):

返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。向上取整

  • public static double floor(double a):

返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。向下取整

  • public static int max(int a,int b):

返回两个 int 值中较大的一个。

  • public static double pow(double a,double b):

返回第一个参数的第二个参数次幂的值。

  • public static double random():

返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。

  • public static int round(float a):

返回最接近参数的 int。

  • public static double sqrt(double a):

返回正确舍入的 double 值的正平方根。

例:

System.out.println("E:"+Math.E);//E:2.718281828459045System.out.println("PI:"+Math.PI);//PI:3.141592653589793System.out.println("abs:"+Math.abs(-10));//abs:10System.out.println("ceil:"+Math.ceil(12.34));//ceil:13.0System.out.println("ceil:"+Math.ceil(12.56));//ceil:13.0System.out.println("floor:"+Math.floor(12.34));//floor:12.0System.out.println("floor:"+Math.floor(12.56));//floor:12.0System.out.println("pow:"+Math.pow(2, 3));//pow:8.0System.out.println("random:"+Math.random());//random:0.8755841978783833System.out.println("round:"+Math.round(12.34));//round:12System.out.println("round:"+Math.round(12.56));//round:13
0 0
原创粉丝点击