目录

  1. 参考链接
  2. 原生抽象类的种类
  3. Number类

    1. 字符串与数值转换
    2. 精确判断数值相等
  4. Math类

    1. PI属性与三角函数相关方法
    2. 舍入运算
    3. 最大和最小
    4. n次方和平方根
    5. 其他

参考链接

原生抽象类的种类

“原生抽象类”是本人自己定义的一个虚构名词,指的是以下几种原生的 Java 抽象类:

  1. Number;
  2. Math;
  3. Character;
  4. String。

Number类

数据类型篇介绍了几种数字类型,其中常用的有intlongdoublebyte,这些内置的数据类型有各自对应的”包装类”——Integer、Long、Double 和 Byte 等等,而这些包装类都属于抽象类 Number 的子类(Number 类属于 java.lang 包)。

以下是一个使用包装类定义变量的实例:

  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. Integer num = 10; // 看起来和"int num = 10"没有半点区别,实际上是两码事
  4. num += 10;
  5. System.out.println(num);
  6. }
  7. }

字符串与数值转换

  1. toString(): 数值转换为字符串然后返回;
  2. parseInt(): 字符串转换为 int 类型数值然后返回。
  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. Integer num = 5;
  4. String str = "5";
  5. System.out.println(num.toString(num) + 1); // "51"
  6. System.out.println(num.parseInt(str) + 1); // 6
  7. }
  8. }

精确判断数值相等

equals==的区别: equals比较的是对象的”内容”,==比较的是对象的”地址”,需要举个例子。

  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. Integer num = 5;
  4. System.out.println(num.equals(5)); // true
  5. }
  6. }

Math类

一些基本的数学运算方法和属性都封装在 Math 类中,Math 的方法都被定义为static形式,通过 Math 类可以在主函数中直接调用。

PI属性与三角函数相关方法

Math.PI即 π 的值,是一个常用的数学常量,说到 π,就联系起几个常用的三角函数运算方法: Math.sin(num)Math.cos(num)Math.tan(num)等。

  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. System.out.println(Math.PI); // 3.141592653589793
  4. System.out.println(Math.sin(Math.PI / 2)); // 1.0
  5. System.out.println(Math.cos(Math.PI)); // -1.0
  6. System.out.println(Math.tan(Math.PI)); // 0.9999999999999999
  7. }
  8. }

舍入运算

  1. Math.ceil(num): 向上舍入,返回结果为 double 类型;
  2. Math.floor(num): 向下舍入,返回结果为 double 类型;
  3. Math.round(num): 四舍五入,返回结果为 int 类型。
  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. System.out.println(Math.ceil(1.5)); // 2.0
  4. System.out.println(Math.floor(1.5)); // 1.0
  5. System.out.println(Math.round(1.5)); // 2
  6. }
  7. }

最大和最小

  1. Math.max(num1, num2): 返回 num1 和 num2 中的最大值;
  2. Math.min(num1, num2): 返回 num1 和 num2 中的最小值。
  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. System.out.println(Math.max(1, 2)); // 2
  4. System.out.println(Math.min(1, 2)); // 1
  5. }
  6. }

n次方和平方根

  1. Math.pow(num1, num2): num1 的 num2 次方,返回值为 double 类型;
  2. Math.sqrt(num): 返回 num 的算术平方根,返回值为 double 类型。
  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. System.out.println(Math.pow(2, 2)); // 4.0
  4. System.out.println(Math.sqrt(4)); // 2.0
  5. }
  6. }

其他

  1. Math.abs(num): 返回绝对值;
  2. Math.random(): 生成一个随机的大于0小于1的 double 类型数值。
  1. public class NativeClass {
  2. public static void main(String[] args) {
  3. System.out.println(Math.abs(-1)); // 1
  4. System.out.println(Math.random());
  5. }
  6. }

  1. ID : 78
  2. DATE : 2018/06/13
  3. AUTHER : WJT20
  4. TAG : Java