• java 基本类型包装类,system类,Math类,Assrays类,大数据运算


    实现字符串与基本数据之间转换

    将字符串转成基本数据类型方法

    例如:将字符串转成成int类型

    1 String str ="123";
    2         int a =Integer.parseInt(str);
    3         System.out.println(a);

    将字符串转成double类型

    1     String str = "2.3";
    2         double d =Double.parseDouble(str);
    3         System.out.println(d);

    将基本数据类型转换成字符串有3种方法

    1.直接和字符串相拼接就是字符串,建议使用方便

    1 int a =123;
    2 String aa =a+"";
    3 System.out.println(aa);

    2.调用String的valueOf方法

     调用String方法的valueof方法转

    1 int a =123;
    2 String aa =String.valueOf(a);
    3 System.out.println(aa);

    3.调用包装类中的toString方法

    1 int a =123;
    2         String str =Integer.toString(a);
    3         System.out.println(str);

    system类

    System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象。System类中的都是static方法,类名访问即可。在JDK中,有许多这样的类。

    currentTimeMillis() 获取当前系统时间与1970年01月01日00:00点之间的毫秒差值

    exit(int status) 用来结束正在运行的Java程序。参数传入一个数字即可。通常传入0记为正常状态,其他为异常状态

    gc() 用来运行JVM中的垃圾回收器,完成内存中垃圾的清除。

    getProperty(String key) 用来获取指定(字符串名称)中所记录的系统属性信息

     arraycopy方法,用来实现将源数组部分元素复制到目标数组的指定位置

     练习一:验证for循环打印数字1-9999所需要使用的时间(毫秒)

    1 long start = System.currentTimeMillis();  //获取当前的毫秒数
    2         for(int i=0;i<=9999;i++){   
    3             System.out.println(i);
    4         }
    5         long end = System.currentTimeMillis();    //获取打印完的毫秒数 
    6         System.out.println((end-start));

    练习二:将src数组中前3个元素,复制到dest数组的前3个位置上

    复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10]

    复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]

    1     int[] src = new int[]{1,2,3,4,5};
    2         int[] dest = new int[]{6,7,8,9,10};
    3         System.arraycopy(src, 0, dest, 0, 3);
    4         for(int a:dest){
    5             System.out.println(a);
    6         }

    练习三:循环生成100-999之间的的三位数并进行打印该数,当该数能被10整除时,结束运行的程序

    1     for(int i=101;i<=999;i++){
    2             if(i%10==0){
    3                 System.exit(0);
    4             }
    5             System.out.println(i);
    6         }

    Math类

    Math 类是包含用于执行基本数学运算的方法的数学工具类,如初等指数、对数、平方根和三角函数。

     1 //绝对值
     2         System.out.println(Math.abs(-99));
     3         //向上取整
     4         System.out.println(Math.ceil(12.2));//13.0
     5         System.out.println(Math.ceil(12.9));//13.0
     6         //向下取整
     7         System.out.println(Math.floor(12.2));//12.0
     8         System.out.println(Math.floor(12.9));//12.0
     9         //取最大值
    10         System.out.println(Math.max(555,222));
    11         //取最小值
    12         System.out.println(Math.min(555, 333));
    13         //取次幂
    14         System.out.println(Math.pow(2, 3));
    15         //取随机数
    16         System.out.println(Math.random());
    17         //四舍五入
    18         System.out.println(Math.round(12.4));
    19         //π
    20         System.out.println(Math.PI);

    Assrays类

    此类包含用来操作数组(比如排序和搜索)的各种方法。需要注意,如果指定数组引用为 null,则访问此类中的方法都会抛出空指针异常NullPointerException

     

     1 public static void method01(){
     2         int[] arr ={3,6,9,11,15};
     3         //返回指定值所对应的索引值(数组必须有序)
     4         //如果不存在,索引值=(-当前的索引值-1)
     5         int i =Arrays.binarySearch(arr, 9);
     6         System.out.println(i);
     7     }
     8     public static void method02(){
     9         //数组升序排序
    10         int[] arr={5,7,8,9,10};
    11         Arrays.sort(arr);
    12         System.out.println(Arrays.toString(arr));
    13         for(int a:arr){
    14             System.out.println(a);
    15         }

     javalong型为最大整数类型,对于超过long型的数据如何去表示呢.Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符.

     

    public static void method(){
            BigInteger b1 =new BigInteger("100000000000000000000000000000000");
            BigInteger b2 =new BigInteger("200000000000000000000000000000000");
            //加法
            System.out.println(b1.add(b2));
            //减法
            System.out.println(b2.subtract(b1));
            //乘法
            System.out.println(b1.multiply(b2));
            //除法
            System.out.println(b2.divide(b1));
        }

    1.1 BigDecimal

      在程序中执行下列代码,会出现什么问题?

        System.out.println(0.09 + 0.01);

        System.out.println(1.0 - 0.32);

        System.out.println(1.015 * 100);

        System.out.println(1.301 / 100);

     doublefloat类型在运算中很容易丢失精度,造成数据的不准确性,Java提供我们BigDecimal类可以实现浮点数据的高精度运算

     

     1 public static void method02(){
     2         BigDecimal bd =new BigDecimal("0.09");
     3         BigDecimal bd1 =new BigDecimal("0.01");
     4         //加法
     5         System.out.println(bd1.add(bd1));
     6         //减法
     7         System.out.println(bd1.subtract(bd));
     8         //乘法
     9         System.out.println(bd.multiply(bd1));
    10         //除法
    11         System.out.println(bd1.divide(bd,BigDecimal.ROUND_FLOOR,3));
    12     }
  • 相关阅读:
    使用python javaSerializationTools模块拼接生成 8u20 Gadget
    随想与心理咨询师和朋友倾诉的区别 po
    Power BI 中怎么在dashboard中展示parameter参数的值? po
    游戏Batman: Arkham Asylum GOTY Launcher 蝙蝠侠 阿甘姆疯人院年度版 WIN10报错:未能加载文件或程ctx.di序集 Microsoft.directX.Direct3D 解决方案 po
    python sublime console 运行 交互输入Yes or No ( SublimeREPL插件) po
    python Browser automation with selenium 网页自动化测试 练习 po
    sublime python3的配置文件python3.sublimebuild存放位置 po
    HTTP 缓存机制介绍
    Visual Studio 2019 常见问题及解决办法
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/wangrongchen/p/9097748.html
Copyright © 2020-2023  润新知