JavaAPI之lang包

雨点打透心脏的1/2处 2022-05-14 15:26 257阅读 0赞

JavaSE学习笔记-JavaAPI之lang包

欢迎大家来我的个人博客:https://www.fxyh97.com/index.php/archives/25/

  • 包装类

  • 数值型(Math,Random,BigInteger,BigDecimal)

  • 字符型(String,StringBuffer,StringBuilder)


  • 包装类:

    • 8大数据类型都有对应的包装类。(集合中不能存基本数据类型)
    • byte,short,int,long,float,double,char,boolean 栈内存
    • Byte,Short,Integer,Long,Float,Double,Character,Boolean
    • 自动装箱:将基本数据类型包装为对象数据类型。

      1. Integer a = 9; //相当于Integer a = Integer.valueOf(9);
    • 自动拆箱:将包装类型转为基本数据类型。

      1. int c = a; //相当于int c = a.intValue();
    • Tnteger类的valueOf方法和parseInt方法的区别?
      • parseInt(String s, int radix); radix进制数,返回int类型
      • parseInt(String s);返回int类型,实际是调用的parseInt(s,10);默认为10进制
      • parseInt可能会报NumberFormatException
      • valueOf(int); 返回Integer对象
      • valueOf(String s);实质是先调用parseInt(s,10)返回int当参数,然后调用valueOf(int)这个方法。返回Integer对象
      • valueOf(String s, int radix);实质是先调用parseInt(s, radix)返回int当参数,然后调用valueOf(int)这个方法。返回Integer对象

  • 数值型(Math)

    • abs(参数)//求绝对值,可以传int,long,float,double类型,返回值也就是所传的类型
    • 取整

      • round(环绕的意思)

        • 传参为double的时候,返回long类型的数

          1. public static long round(double a) {...}
        • 传参为float的时候,返回int类型的数,传非double和boolean的基本数据类型都是是调用这个方法。

          1. public static int round(float a) {...}
        • 当round传参大于0的时候,进行四舍五入,当round传参小于0是,直接入,即-1.4返回成-1
        • 例如

          1. System.out.println("Math.round(1.4):" + Math.round(1.4));
          2. System.out.println("Math.round(1.5):" + Math.round(1.5));
          3. System.out.println("Math.round(-1.4):" + Math.round(-1.4));
          4. System.out.println("Math.round(-1.5):" + Math.round(-1.5));
          5. 打印结果:
          6. Math.round(1.4):1
          7. Math.round(1.5):2
          8. Math.round(-1.4):-1
          9. Math.round(-1.5):-1
      • ceil天花板数

        • 只要小数部分大于0,就入
        • 返回double类型,能传出boolean以外的所有基本数据类型

          1. public static double ceil(double a) {...}
        • 例如

          1. System.out.println("Math.ceil(1.0):" + Math.ceil(1.0));
          2. System.out.println("Math.ceil(1.4):" + Math.ceil(1.4));
          3. System.out.println("Math.ceil(1.5):" + Math.ceil(1.5));
          4. System.out.println("Math.ceil(1.5f):" + Math.ceil(1.5f));
          5. System.out.println("Math.ceil(-1.0):" + Math.ceil(-1.0));
          6. System.out.println("Math.ceil(-1.4):" + Math.ceil(-1.4));
          7. System.out.println("Math.ceil(-1.5):" + Math.ceil(-1.5));
          8. 打印结果:
          9. Math.ceil(1.0):1.0
          10. Math.ceil(1.4):2.0
          11. Math.ceil(1.5):2.0
          12. Math.ceil(1.5f):2.0
          13. Math.ceil(-1.0):-1.0
          14. Math.ceil(-1.4):-1.0
          15. Math.ceil(-1.5):-1.0
      • floor地板数

        • 只要小数部分大于0,就舍
        • 返回double类型,能传出boolean以外的所有基本数据类型

          1. public static double floor(double a) {...}
        • 例如:

          1. System.out.println("Math.floor(1.0):" + Math.floor(1.0));
          2. System.out.println("Math.floor(1.1):" + Math.floor(1.1));
          3. System.out.println("Math.floor(1.9):" + Math.floor(1.9));
          4. System.out.println("Math.floor(1.1f):" + Math.floor(1.1f));
          5. System.out.println("Math.floor(-1.0):" + Math.floor(-1.0));
          6. System.out.println("Math.floor(-1.1):" + Math.floor(-1.1));
          7. System.out.println("Math.floor(-1.9):" + Math.floor(-1.9));
          8. 打印结果:
          9. Math.floor(1.0):1.0
          10. Math.floor(1.1):1.0
          11. Math.floor(1.9):1.0
          12. Math.floor(1.1f):1.0
          13. Math.floor(-1.0):-1.0
          14. Math.floor(-1.1):-2.0
          15. Math.floor(-1.9):-2.0
    • pow 幂运算

      • 传非boolean以外的所有基本数据类型,返回double类型

        public static double pow(double a, double b) {…}

    • sqrt 开根号

      • 传非boolean以外的所有基本数据类型,返回double类型

        public static double sqrt(double a) {…}

    • random :产生0到1的double类型小数,包括0,不包括1。

      1. public static double random() {...}
  • 数值型(BigInteger)

    • 使用new关键字创建对象,一般调用形参为String的构造方法。如:new BigInteger(“” + 1);
    • 加减乘除取模见下面的例子:

      1. BigInteger b = new BigInteger("" + 100);
      2. System.out.println("原来:" + b.toString());
      3. b = b.multiply(b);//乘
      4. System.out.println("乘法:" + b.toString());
      5. b = b.add(b);//加
      6. System.out.println("加法:" + b.toString());
      7. b = b.divide(new BigInteger("3"));//除
      8. System.out.println("除法:" + b.toString());
      9. b = b.subtract(new BigInteger("3"));//减法
      10. System.out.println("减法:" + b.toString());
      11. b = b.mod(new BigInteger("3"));//取模
      12. System.out.println("取模:" + b.toString());
      13. 打印结果:
      14. 原来:100
      15. 乘法:10000
      16. 加法:20000
      17. 除法:6666
      18. 减法:6663
      19. 取模:0

      期间出现了一个小毛病,我直接b.multiply(b);//乘,没有写成b = b.multiply(b);//乘,一直没有效果

    • 求30的阶乘:

      • 第一种方法:

        1. public static BigInteger funBig(int n) {
        2. BigInteger sum = new BigInteger("" + 1);
        3. for (int i = 1; i <= n; i++) {
        4. sum = sum.multiply(new BigInteger("" + i));
        5. }
        6. return sum;
        7. }
      • 第二种方法:使用一行代码实现:

        1. public static BigInteger fBigInteger(int n) {
        2. return n <= 1 ? new BigInteger("" + 1) : new BigInteger("" + n).multiply(fBigInteger(n - 1));
        3. }
  • 字符型:

    • String,StringBuilder,StringBuffer的详细解释,见我的另外一篇博客
      https://blog.csdn.net/qq_37872792/article/details/83451582
    • 这里我们就说些常用的方法吧。
    • String https://blog.csdn.net/qq_37872792/article/details/83473207
    • StringBuilder

      1. StringBuilder sb = new StringBuilder("abcbabc");
      2. System.out.println("最初:" + sb);
      3. sb.append("def");
      4. System.out.println("append(def):" + sb);
      5. sb.delete(1, 3);//删除从第一个字符开始,到第三个字符,不包括第三个字符,包头不包尾
      6. System.out.println("delete(1,3):" + sb);
      7. sb.reverse();//反转
      8. System.out.println("反转:" + sb);
      9. int indexOf = sb.indexOf("b");//第一个b
      10. System.out.println("从前往后找b:" + indexOf);
      11. int lastIndexOf = sb.lastIndexOf("b");//最后一个b
      12. System.out.println("从后往前找b:" + lastIndexOf);
    • StringBuffer和StringBuilder类似,唯一的不同就是它是线程安全的。

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java.lang

    java.lang包 java中的默认包只有java.lang.\JDK也只默认导入这个包,这个包下的子包也需要程序猿手动的导入,至于在编程的时候自己没有创建package