Java学习笔记 day17

1 常用类

包装类 Wrapper

针对八种基本数据类型相应的引用类型 -> 包装类

有了类的特点,就可以调用类中的方法

装箱和拆箱

装箱就是将基础数据类型转换为对应的包装器类型;

拆箱就是将包装器类型转换为对应的基础数据类型;

让基本类型具备对象的特征,实现更多的功能

//boolean原生类型自动装箱成Boolean

    public static Boolean valueOf(boolean b) {

        return (b ? TRUE : FALSE);

    }

    //byte原生类型自动装箱成Byte

    public static Byte valueOf(byte b) {

        final int offset = 128;

        return ByteCache.cache[(int)b + offset];

    }

    //short原生类型自动装箱成Short

    public static Short valueOf(short s) {

        final int offset = 128;

        int sAsInt = s;

        if (sAsInt >= -128 && sAsInt <= 127) { // must cache

            return ShortCache.cache[sAsInt + offset];

        }

        return new Short(s);

    }

    //char原生类型自动装箱成Character

    public static Character valueOf(char c) {

        if (c <= 127) { // must cache

            return CharacterCache.cache[(int)c];

        }

        return new Character(c);

    }

    //int原生类型自动装箱成Integer

    public static Integer valueOf(int i) {

        if (i >= IntegerCache.low && i <= IntegerCache.high)

            return IntegerCache.cache[i + (-IntegerCache.low)];

        return new Integer(i);

    }

    //long原生类型自动装箱成Long

    public static Long valueOf(long l) {

        final int offset = 128;

        if (l >= -128 && l <= 127) { // will cache

            return LongCache.cache[(int)l + offset];

        }

        return new Long(l);

    }

    //double原生类型自动装箱成Double

    public static Double valueOf(double d) {

        return new Double(d);

    }

    //float原生类型自动装箱成Float

    public static Float valueOf(float f) {

        return new Float(f);

    }

包装类 和 String互相转换

Integer -> String

Integer i = 100;

①String str1 = i + " ";

②i.toString.var

③String str3 = String.valueOf(i);

String -> Integer

String str4 = "12345";

①Intege i2 = Integer.parseInt(str4);

②Intege i3 = new Integer(str4);

String

String是final类 不能被其他类继承

String对象用于保存字符串 也就是一组字符序列

字符串常量用双引号括起来

使用unicode编码 一个字符占两个字节

String 实现的接口

Serializable String可以串行化(可以在网络传输)

Comparable String 可以比较

CharSequence String

Object

创建对象

① 直接赋值 String s = "   xxx   ";

②调用构造器 String s2 = new String("xxx");

StringBuffer

StringBuild

Math

Date Calendar LocalDate

System

Arrays

BigInteger BigDecimal

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值