【Java 包装类】

78 篇文章 0 订阅

对应关系

基本类型包装类父类
byteByteNumber
shortShortNumber
intIntegerNumber
longLongNumber
floatFloatNumber
doubleDoubleNumber
charCharacterObject
booleanBooleanObject

Integer

package com.yuzhenc.common;

/**
 * @author: yuzhenc
 * @date: 2022-02-27 15:47:33
 * @desc: com.yuzhenc.common
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) {
        //Integer.MAX_VALUE int类型能表示的最大值
        System.out.println(Integer.MAX_VALUE);//2147483647
        //Integer.MIN_VALUE int类型能表示的最小值
        System.out.println(Integer.MIN_VALUE);//-2147483648
        //物极必反
        System.out.println(Integer.MAX_VALUE+1);//-2147483648
        System.out.println(Integer.MIN_VALUE-1);//2147483647

        //Integer的构造器
        Integer num1 = new Integer(12);
        Integer num2 = new Integer("12");
        //Integer num3 = new Integer("abcd");//报错

        Integer num4 = 12;//自动装箱
        int num5 = new Integer(12);//自动拆箱
        Integer num6 = new Integer(12);
        int num7 = 12;
        Integer num8 = 256;
        Integer num9 = 256;
        Integer num10 = 12;

        //比较的是对象的地址值
        System.out.println(num1 == num6);//false
        //基本类型比较值
        System.out.println(num5 == num7);//true
        //如果自动装箱值在-128~127之间,那么比较的就是具体的数值,否则比较的就是对象的地址
        //底层有一个缓冲数组,-128~127中的数,直接从该数组中返回
        System.out.println(num4 == num10);//true
        System.out.println(num8 == num9);//false
        //Integer重写了Object的equals()方法,比较的是值
        System.out.println(num1.equals(num2));//true
        //x.compareTo(y) return (x<y) ? -1 : ((x==y) ? 0 : 1)
        //注:x和y都是只得int的值
        System.out.println(num1.compareTo(num2));//0
        System.out.println((num1<num2) ? -1 : ((num1==num2) ? 0 : 1));//1
        System.out.println((num1.intValue()<num2.intValue()) ? -1 : ((num1.intValue()==num2.intValue()) ? 0 : 1));
        //Integer.parseInt() String转int
        System.out.println(Integer.parseInt("12"));//12
        //toString() Integer转String
        System.out.println(num1.toString());//12
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sqlboy-yuzhenc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值