记录一下Java包装类的常用方法

package com.atguigu.base_type;

import org.junit.Test;

/**
 * 测试java八大基础类型对应的引用类型的方法
 * byte、char(2字节)、short、int、long、float(4字节)、double
 *      从左往右类型自动转换,从右往左需要强制转换。强制转换可能精度丢失。
 * boolean不能类型转换。
 *  装箱:Integer num1=12;或者 Integer num2=new Integer(12);
 *  拆箱:int num3=num1;  或者 int num4=num3.intValue();
 *  所有的包装类都被final修饰,不允许被继承。和String类一样,所有包装类都重写了equals()方法,调用时比较的是内容。
 *  和String类一样,包装类不可变。
 *  除浮点型外的六种基本数据类型都实现了对象常量池,除Boolean外,其他的五种会在常量池中保存着-128~127范围内的对象。
 *      Integer a=12; Integer b=12;   只要数值在-128~127之间,a和b就是等价的,
 *      这对于Character/Byte/Short/Long/Integer都是成立的,对于Float和Double不成立。   
 */
public class MyBaseTypeTest {

    @Test
    public void testCharacter(){
        //判断
        System.out.println(Character.isLetter('A'));//是否为字母字符
        System.out.println(Character.isDigit('1'));//是否为数字字符
        System.out.println(Character.isLetterOrDigit('A'));//是否为字母或数字字符
        System.out.println(Character.isUpperCase('A'));//是否为大写字母
        System.out.println(Character.isLowerCase('A'));//是否为小写字母
        System.out.println(Character.isWhitespace(' '));//是否为空白字符
    }

    /**
     * 本方法中调用的所有的Integer方法,在Long类中都有。
     */
    @Test
    public void testIntegetrAndLong(){
        Integer a = 12345;
        Integer b = 127;
        //比较大小
        System.out.println(a.equals(b));
        System.out.println(Integer.max(a,b));
        System.out.println(Integer.min(a,b));
        System.out.println(Integer.sum(a,b));
        System.out.println(Integer.compare(a,b));//a-b大于0返回1,a=b返回0,否则返回-1
        System.out.println(a.compareTo(b));//a-b大于0返回1,a=b返回0,否则返回-1。内部调用了compare
        System.out.println(a.compareUnsigned(a,b));//与compareTo不同的是,会将负数转成32位整数再比较
        System.out.println("========================================");

        /**
         * 字符串转数值,decode可以识别八进制和十六进制,valueOf和parseInt则不行。
         * getInteger是根据指定的名称得到系统属性的整数值,系统属性可通过System.getProperty("java.lang.String")方法访问
         *
         */
        System.out.println(Integer.decode("10")+"======"+Integer.decode("010")+"======"+Integer.decode("0x10"));
        System.out.println(Integer.valueOf("10")+"======"+Integer.valueOf("010"));
        System.out.println(Integer.parseInt("10")+"======"+Integer.parseInt("010"));
        System.out.println(Integer.getInteger(System.getProperty("java.lang.String")));
        System.out.println("========================================");

        /**
         * 数值转字符串、hashcode
         */
        System.out.println(a.toString());
        System.out.println(Integer.toString(10,3));//第二个参数为进制
        System.out.println(Integer.hashCode(a));
        System.out.println(a.hashCode());
        System.out.println("========================================");

        //进制转换
        System.out.println(Integer.toBinaryString(a));//转为二进制
        System.out.println(Integer.toOctalString(a));//转为八进制
        System.out.println(Integer.toHexString(a));//转为十六进制
        System.out.println("========================================");

        //二进制计算
        System.out.println(Integer.reverse(a));//将该数值的32位二进制表示看作一个整体进行高低位翻转
        System.out.println(Integer.reverseBytes(a));//将该数值的32位二进制表示每八位(一个字节)看作一个整体进行高低位翻转
        System.out.println(Integer.rotateLeft(8,3));//左移
        System.out.println(Integer.rotateRight(8,3));//右移
        System.out.println("========================================");

        //基础类型转换:小类型到大类型自动转换,大类型到小类型前置转换(可能丢失精度)
        System.out.println(a.byteValue());//相当于(byte)a,将a转为byte类型,只保留最低八位。
        System.out.println(a.shortValue());//相当于(short)a,将a转为short类型,只保留最十六位。
        System.out.println(a.intValue());//相当于(int)a,将a转为int类型,只保留最低三十二位。
        System.out.println(a.longValue());//相当于long s=a,将a转为long类型。
        System.out.println(a.floatValue());//相当于float s=a,将a转为float类型。
        System.out.println(a.doubleValue());//相当于double s=a,将a转为double类型。
        System.out.println("========================================");

        //正负性
        System.out.println(Integer.signum(a));//a为负,返回-1;为零,返回0;为正,返回1.用来判断int值得正负性
    }

    /**
     * 本方法调用的所有Float方法,在Double都有对应的。
     */
    @Test
    public void testFloatAndDouble(){
        Float a = 12.345f;
        Float b = 127.8f;
        //比较大小
        System.out.println(a.equals(b));
        System.out.println(Float.max(a,b));
        System.out.println(Float.min(a,b));
        System.out.println(Float.sum(a,b));
        System.out.println(Float.compare(a,b));//a-b大于0返回1,a=b返回0,否则返回-1
        System.out.println(a.compareTo(b));//a-b大于0返回1,a=b返回0,否则返回-1。内部调用了compare
        System.out.println("========================================");

        //字符串、数值转换。
        System.out.println(Float.valueOf("10")+"======"+Float.valueOf("010"));
        System.out.println(Float.parseFloat("10")+"======"+Float.parseFloat("010"));
        System.out.println(a.toString());
        System.out.println(Float.toString(a));//没有第二个参数
        System.out.println(Float.hashCode(a));
        System.out.println(a.hashCode());
        System.out.println("========================================");

        //判断
        System.out.println(Float.isFinite(1.0f));//如果参数是有限浮点值,则返回true;如果是NAN或无穷,返回false
        System.out.println(Float.isInfinite(1.0f));//如果参数是无穷,返回true;否则返回false
        System.out.println(Float.isNaN(a));//如果参数非float浮点数,发怒ifalse,否则true.
        System.out.println("=======================================");

        //基础类型转换:小类型到大类型自动转换,大类型到小类型前置转换(可能丢失精度)
        System.out.println(a.byteValue());//相当于(byte)a,将a转为byte类型,只保留最低八位。
        System.out.println(a.shortValue());//相当于(short)a,将a转为short类型,只保留最十六位。
        System.out.println(a.intValue());//相当于(int)a,将a转为int类型,只保留最低三十二位。
        System.out.println(a.longValue());//相当于long s=a,将a转为long类型。
        System.out.println(a.floatValue());//相当于float s=a,将a转为float类型。
        System.out.println(a.doubleValue());//相当于double s=a,将a转为double类型。
        System.out.println("========================================");

        //进制转换
        System.out.println(Float.toHexString(a));//转为十六进制
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二百四十九先森

你的打赏是我努力的最大动力~

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

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

打赏作者

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

抵扣说明:

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

余额充值