【JavaEE进阶系列 | 从小白到工程师】Math类与System类的常用方法使用

2年前 (2022) 程序员胖胖胖虎阿
197 0 0

文章目录

  • 💨更多相关知识👇
    • 一、Math类
      • 1、Math 类概述
      • 2、Math 类的常用方法
        • 🍂 public static int abs (int a)
        • 🍂public static double ceil(double a)
        • 🍂public static double floor(double a)
        • 🍂public static int round(float a)
        • 🍂public static double pow(double a,double b)
        • 🍂 public static int max(int a,int b)
        • 🍂 public static int min(int a,int b)
        • 🍂 public static double random()
    • 二、System类
      • 1、System 类概述
      • 2、System 类的常用方法
        • 🌿 public static void exit(int status)
        • 🌿 public static long currentTimeMillis()
        • 🌿 public static void arraycopy(Object src,int srcPos, Object dest, int destPos, int length)
  • 作者:KJ.JK

🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
 
🍂个人博客首页: KJ.JK
 
欢迎大家点赞👍收藏💖评论💬关注🔒
 
💖源码获取 | 💻学习交流 | 🤝商务合作 | 💨私信作者


💨更多相关知识👇

💖JavaEE中的静态方法定义、方法重载要求、return作用详解

💖List接口的常用方法,精华总结

💖JavaEE里面的所有内部类详细介绍与使用

💖ArrayList集合中的常用成员方法

💖Java中String的split切割字符串方法详解

💖String中的replace的四个方法

💖JavaEE中的Stream流的常用方法

💖JavaEE中的Stream流知识点使用,精华总结,一文直接上手

💖JavaWeb中的过滤器(Filter)和监听器(Listener)区别

💖JavaEE中史上最全String类入门介绍文章,学习StringAPI这一篇即可


一、Math类


1、Math 类概述

          * Math是数学工具类,包含执行数学运算的各种方法
          
          * Math类的构造方法是私有的,无法创建对象,通过类名即可调用方法

2、Math 类的常用方法


🍂 public static int abs (int a)

//public static int abs(int a):返回参数的绝对值
                System.out.println(Math.abs(-1));  //1

🍂public static double ceil(double a)

//public static double ceil(double a):     返回大于或等于参数的最小double值,等于一个整数(向上取值)(变大)
              System.out.println(Math.ceil(12.3));   //13.0
              System.out.println(Math.ceil(45.5));  //46.0

🍂public static double floor(double a)

//public static double floor(double a): 返回小于或等于参数的最大double值,等于一个整数(向下取值)(变小)
             System.out.println(Math.floor(12.5));  //12.0
             System.out.println(Math.floor(39.5));  //39.0

🍂public static int round(float a)

//public static int round(float a):  按照四舍五入返回最接近参数的int
             System.out.println(Math.round(4.5));   //5
             System.out.println(Math.round(4.2));  //4

🍂public static double pow(double a,double b)

//public static double pow(double a,double b):返回a的b次幂的值
        System.out.println(Math.pow(2,2));    //4.0
        System.out.println(Math.pow(5,3));  //125.0

🍂 public static int max(int a,int b)

//public static int max(int a,int b)  :    返回两个int值中的较大值
            System.out.println(Math.max(2,5));  //5

🍂 public static int min(int a,int b)

// public static int min(int a,int b)   :  返回两个int值中的较小值
              System.out.println(Math.min(4,40)); //4

🍂 public static double random()

//public static double random(): 返回值为double的正值,[0.0,1.0) ,返回0.0到1.0(不包括1.0)的随机数 
                  System.out.println(Math.random());

🔥系列热门专栏🔥:
 
⚡《JavaEE进阶序列 | 从小白到工程师》系列文章⚡
 
⚡《JavaEE项目实战》系列文章⚡
 
⚡《JavaSwing项目合集》系列文章⚡
 
⚡《数据分析中的pandas》系列文章⚡


二、System类


1、System 类概述

            * System位于java.lang包下,代表"当前Java程序的运行平台"
            
            * System类中提供了大量静态方法,可以获取与系统相关的信息或执行系统级操作

2、System 类的常用方法


🌿 public static void exit(int status)

//public static void exit(int status):终止当前运行的Java虚拟机,非零表示异常终止

                     while (true){
                      System.out.println("hello");
                      System.exit(0);

                         }
/*     
   不管status为何值都会退出程序(即结束当前正在运行的java虚拟机),
   但是只有在status等于0,即System.exit(0)的时候整个程序正常退出
   当status为除0之外的数字时都视为程序的不正常退出
*/

【JavaEE进阶系列 | 从小白到工程师】Math类与System类的常用方法使用


🌿 public static long currentTimeMillis()

// public static long currentTimeMillis():返回当前时间(以毫秒为单位)

                  System.out.println(System.currentTimeMillis());

// 从1970年1月1日开始算
// 一秒等于一千毫秒

【JavaEE进阶系列 | 从小白到工程师】Math类与System类的常用方法使用


🌿 public static void arraycopy(Object src,int srcPos, Object dest, int destPos, int length)

//public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) :实现数组拷贝  (原数组, 起始索引, 新数组, 起始索引, 拷贝个数)

        int [] arr={11,22,33,44};
        int [] arr1=new int[4];
        System.arraycopy(arr,2,arr1,0,2);

       /*
            原数组arr  原数组起始索引2   新数组arr1  新数组起始索引0     拷贝2个      其他空白的位置补数组的默认值
                                                                              补多少个看新数组的长度
       */

        System.out.println(Arrays.toString(arr1)); //[33, 44, 0, 0]

-----------------------------------------------------------------------------------------------------------

        int []oldarr={11,22,33,44,55,66};
        int []newarr=new int[10];

        System.arraycopy(oldarr,2,newarr,4,3);

        System.out.println(Arrays.toString(newarr)); //[0, 0, 0, 0, 33, 44, 55, 0, 0, 0]

【JavaEE进阶系列 | 从小白到工程师】Math类与System类的常用方法使用


【JavaEE进阶系列 | 从小白到工程师】Math类与System类的常用方法使用


作者:KJ.JK

文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习

相关文章

暂无评论

暂无评论...