包装类

包装类

包装类都是在lang包中,不需要导包

包装类就是把基本数据类型包装成引用数据类型

byte short int long float double char boolean void
Byte Short Integer Long Float Double Character Boolean Void
  • Void类

Void类是最终类,没有子类,构造方法私有,不能创建对象

public static Void method() {
    return null;
}
  • 数值类型

Byte Short Integer Long Float Double

它们有共同的父类 Number

自动装箱

自动装箱是jdk5的特性,底层调用的是valueOf(类型)

对于Byte来说,在-128到127之间返回同一个对象。不会超出范围

对于Short、Integer、Long来说,在-128到127之间,返回的是同一个对象,超出这个范围,返回新的对象

对于Float、Double来说,每次都是创建新的对象

对于Character来说,0-127返回相同对象,否则返回新的对象

对于Boolean来说,要么返回TRUE、要么返回FALSE

自动拆箱

jdk5之后 可以自动拆箱:

把对应的引用数据类型转换成基本数据类型

底层调用的是xxxValue()方法

public static Void method() {

    int x = integer;
    // 当包装类和对应的基本类型进行运算的时候,会自动拆箱
    Integer integer1 = 998;
    if (integer1 <= 1000) {
        System.out.println("哈哈哈");
    }

    Double dou = 100.0;
    double d1 = dou.doubleValue();

    // Integer类型的最大值和最小值
    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MIN_VALUE);
    System.out.println(Integer.max(10, 20));
    System.out.println(Integer.sum(10, 20));

    // 方法已过期
//        Integer integer2 = new Integer("123");
//        System.out.println(integer2 == 123);
    // 推荐使用的方法
    // 把字符串转换成对应的基本数据类型
    int anInt = Integer.parseInt("134");
    double v = Double.parseDouble("3.14");
    long l = Long.parseLong("76");
    boolean aTrue = Boolean.parseBoolean("TRue");
    // 字符串如何转换成char类型? 可以先转换成字符数组,再使用charAt方法获取字符
    System.out.println(aTrue);
    System.out.println(l);
    // 注意:包装类产生的对象的哈希码值是固定不变的
    // null的哈希码值规定为0
    System.out.println(in.hashCode());
    System.out.println(Double.hashCode(3.14));
    System.out.println(Long.hashCode(134L));
    System.out.println(Character.hashCode('a'));
    System.out.println(Boolean.hashCode(true));

    // NaN   :  not a number
    // NaN 自己和自己都不相等
    System.out.println((0.0 / 0) == (0.0 / 0));
    // 可以通过Double来判断NaN
    System.out.println(Double.isNaN((0.0 / 0)));

    System.out.println(new Object());

    // 进制转换
    // 转换成二进制
    System.out.println(Integer.toBinaryString(10));
    // 转换成八进制
    System.out.println(Integer.toOctalString(100));
    // 转换成十六进制
    System.out.println(Integer.toHexString(100101010));
}

数学类 Math

public static void method() {
    // 圆周率
    System.out.println(Math.PI);
    // 自然指数
    System.out.println(Math.E);

    // 绝对值
    System.out.println(Math.abs(-10));
    // 立方根
    System.out.println(Math.cbrt(27));
    // 平方根
    System.out.println(Math.sqrt(9));
    // 向上取整
    System.out.println(Math.ceil(3.74));
    // 向下取整
    System.out.println(Math.floor(-3.4));
    // 加权函数  a的b次方
    System.out.println(Math.pow(3, 4));
    // 获取随机数  [100000,1000000)
    System.out.println(Math.random());
    // 四舍五入
    System.out.println(Math.round(3.5));
}

BigDecimal

BigDecimal用于精确计算的类。

使用这个类,要求参数以字符串形式传递,底层按位计算

public static void method() {
    BigDecimal bigDecimal = new BigDecimal("3.0");
    BigDecimal bigDecimal1 = new BigDecimal("2.0");
    // 加法
    System.out.println(bigDecimal.add(bigDecimal1));
    // 减法
    System.out.println(bigDecimal.subtract(bigDecimal1));
    // 乘法
    System.out.println(bigDecimal.multiply(bigDecimal1));
    // 除法  要求能够获取精确结果
    System.out.println(bigDecimal.divide(bigDecimal1));
}

BigInteger

可以进行大量数字的存储和计算,最多可以存储67,108,864位

public static void method() {
    BigInteger bigInteger1 = new BigInteger("987654334326756");
    BigInteger bigInteger2 = new BigInteger("98765435");
    System.out.println(bigInteger1.multiply(bigInteger2));
}

DecimalFormat

数字格式转换

public static void method() {
    double d = 2.19 * 0.88;
    System.out.println(d);

    // 创建对象指定数字显示的格式
    // 0代表占位符,表示一位数字,如果这一位没有数字,用0填充
    DecimalFormat decimalFormat = new DecimalFormat("00.00");
    System.out.println(decimalFormat.format(2.8999));

    DecimalFormat decimalFormat1 = new DecimalFormat("#0.00");
    // #是一个占位符,表示一位数字,这一位如果没有数字,那么就不填充内容
    System.out.println(decimalFormat1.format(2.8999));

    double d2 = 567546436546342L;
    // 科学计数法
    DecimalFormat decimalFormat2 = new DecimalFormat("0.00E0");
    System.out.println(decimalFormat2.format(d2));
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇