包装类&Math

包装类&Math

  • 为了让基本数据类型具有对象的性质 丰富了基本数据的操作

  • 包装类都是首字母大写 除了Integer Character

  • 四类八种基本类型的包装类

1.常用方法

1.Integer常用方法

public static void main(String[] args){
 //构造方法
        Integer in1 = new Integer(12);
        Integer in2 = new Integer("12");
        int i = in2 + 12;
        System.out.println(i);
        //1,parseInt() 把数值字符串转成对应的int值 64+16+4+1 使用2进制转换
        Integer i1 = Integer.parseInt("01010101", 2);
        System.out.println(i1);
        //2,valueOf() 把数值字符串转成对应的Integer值
        int i2 = Integer.valueOf("00100",2); // 4
        System.out.println(i2);
    //valueOf()会做封装,parseInt则直接返回int值, 一般用parseInt 除非非要返回Integer值,要不然会有封装拆箱,性能会有浪费;
        // compare比较 返回值是正数表示第一个值大 0表示相等 负数表示第二个值大
        int compare = Integer.compare(10, 20);
        System.out.println("compare " + compare);
        // int的表数范围的最大值
        System.out.println(Integer.MAX_VALUE);
        // int的表数范围的最小值
        System.out.println(Integer.MIN_VALUE);
}

2.包装类型缓冲池

  • 整数 byte short int long -128-127
  • 字符型 所有字符都在里面
  • 不包含任何浮点数
 public static void constantPool() {
        Integer in1 = new Integer(12);
        Integer in2 = new Integer(12);
        // -128-127
        Integer in3 = 127;
        Integer in4 = 127;
        Long l1 = (long) 12;
        Long l2 = (long) 12;
     //在Integer范围内 -128-127  所有都在包装类型缓冲池中
        System.out.println(l1 == l2);//true
        System.out.println("in3VS in4 " + (in3 == in4));//true
     //in1 in2都重新new了新对象,都有属于自己的指向位置 ==比较的地址,所以不相等
        System.out.println(in1 == in2);//false
        Character c1 = 'a';
        Character c2 = 'a';
        Float f1 = (float) 1.0;
        Float f2 = (float) 1.0;
     //字符型,所有字符都在里面
        System.out.println("字符是否相等" + (c1 == c2));//true
     //包装类型缓冲池中不包括任何浮点数所以为false
        System.out.println(f1 == f2);
    }

3.不可变的对象

public static void method() {
        Integer in1 = 10;
     // 一旦改值 就是新的对象
        in1 = 20;
        System.out.println(in1);
    }

4.进制和位移

 int b = 24;// 00011000
        // 位移运算符  0011   11000
        System.out.println(b << 3);//向右移动三位    00011000-->00000111相当于乘以 2的三次方  
        System.out.println(b >> 3); //向左移动三位   00011000-->11000000相当于除以 2 的三次方
        //十进制
        System.out.println(12);
        // 八进制表现形式 0
        System.out.println(052);// 2+5*8 =42
        // 十六进制 0X/x 10-A 11-B 15-F F 1111
        System.out.println(0XFA);//250  10+15*16=250
        // 二进制 0B/b
        System.out.println(0B101);//2的0次方+2的2次方 = 5
        int a = 0x52;
        System.out.println(a);//2+5*16=82
    }

5.Character类

 public static void wrapCharacter() {
	    Character ch = 'a';//0-9
        boolean flag1 = Character.isDigit(ch);//判断是否数字
        boolean flag2 = Character.isUpperCase(ch);//A-z  是否大写
        boolean flag3 = Character.isLetter(ch);//是否是字母
        //转小写
        char c = Character.toLowerCase(ch);
        System.out.println(flag1);
        System.out.println(flag2);
        System.out.println(flag3);*/
    }

2.Math

  • 内部定义了和数学运算相关的方法 都是静态方法static
public static void main(String[] args) {
//        π
        double pi = Math.PI;
        System.out.println(pi);//3.1415926
//        3的4次方
        double pow = Math.pow(3,4);
        System.out.println(pow);//81
//        开跟号
        double sqrt = Math.sqrt(16);
        System.out.println(sqrt);//4.0
//        求绝对值
        int abs = Math.abs(-12);
        System.out.println(abs);//12
//        sin  求正弦  cos 求余弦
        Math.random();//随机数
//        向上取最小整
       double ceil = Math.ceil(4.5);
        System.out.println(ceil);//5.0
//        向下取整
        double floor = Math.floor(4.5);
        System.out.println(floor);//4.0
//        四舍五入
        long round = Math.round(4.6);
        System.out.println(round);//5.0
//        最大值和最小值
        double max = Math.max(2.3, 2.4);
        double min = Math.min(2.3, 2.4);
        System.out.println(max);//2.4
        System.out.println(min);//2.3
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值