4.2常用对象API(基本数据类型对象 包装类)

1.基本数据类型对象包装类:
为了方便操作基本数据类型值,将其封装成了对象,在对象中定义了属性和行为,丰富了该数据的操作。

    byte :      Byte
    short :     Short
    int :              Integer
    long :      Long
    float:      Float
    double:     Double
    char:              Character
    boolean:    Boolean

2.和String类型相互转换:

基本类型 转换 String:

1.基本数据类型数值  + "" 
2.用String类中的静态方法valuOf(基本类型数值)



String 转换 基本类型:

1.使用包装类中的静态方法 xxx  parseXxx("xxx类型的字符串"):

int parseInt("intstring");          
long parseLong("longstring");
boolean parseBoolean("true/false");
...

只用Character没有parse方法,不需要。

2.如果字符串被Integer进行了封装。
可以用另一个非静态的方法,intValue():

Integer i = new Integer("123");
i.intValue();

3.进制转换:

1.十进制  转换 其他进制:
String  toBinaryString(int i); 转换成二进制
        toOctalString(int i); 转换成八进制
        toHexString(int i); 转换成十六进制
        toString(int i, int radix); 转换成指定进制

2.其他进制 转换 十进制:
int     parseInt(String s, int radix);  

(Integer  valueOf(String s, int radix);  返回的值不能运算,还是字符串。)

4:数字对象的运算:

    //数字对象的运算:
    Integer  a = new Integer("3");
    Integer b = new Integer(3);
    Integer c = new Integer(1);

    System.out.println(a==b);   //false
    System.out.println(a.equals(b));   //true , 比较数值是否相同
    System.out.println(c.compareTo(a)); //比较

5.JDK1.5自动装箱拆箱:

int num = 4;
num = num + 5;

Integer i =4;  //等效,Integer i = new Integer(4);  自动装箱,就是简化书写。 
            //自动装箱和不同赋值相比,多了一个空null。
            //所以要先判断是否为null。因为空类型在拆箱的时候会报空指针异常。
i = i + 6;   //等效i = new Integer(i.intValue() + 6); 自动装箱,拆箱(i.intValue())。

show(55);

public static void show(Object a){  //Object a = new Ingeter(55);  装箱,多态

}

面试:自定装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间。

Integer a = new Integer(127);
Integer b = new Integer(127);

System.out.println(a==b);  //false
System.out.println(a.equals(b));  //true


Integer x = 127;
Integer y = 127;

System.out.println(x==y);   //true, 自定装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间。
System.out.println(x.equals(y));  //true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值