Java 数字

前言

1、数字类

  • Number 数字抽象类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package java.lang;

    /**
    * The abstract class {@code Number} is the superclass of platform
    * classes representing numeric values that are convertible to the
    * primitive types {@code byte}, {@code double}, {@code float}, {@code
    * int}, {@code long}, and {@code short}.
    *
    * @author Lee Boynton, Arthur van Hoff
    * @jls 5.1.2 Widening Primitive Conversions
    * @jls 5.1.3 Narrowing Primitive Conversions
    * @since JDK1.0
    */

    public abstract class Number implements java.io.Serializable {

    }
  • 数字封装类 ByteShortIntegerLongFloatDouble,这些类都是抽象类 Number 的子类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package java.lang;

    public final class Byte extends Number implements Comparable<Byte> {
    }

    public final class Short extends Number implements Comparable<Short> {
    }

    public final class Integer extends Number implements Comparable<Integer> {
    }

    public final class Long extends Number implements Comparable<Long> {
    }

    public final class Float extends Number implements Comparable<Float> {
    }

    public final class Double extends Number implements Comparable<Double> {
    }
  • 基本数字类型 与 数字类

类型 数据类型 封装类 尺寸 长度 默认值 范围
整型 byte Byte 1 字节 8 位 0 -128 ~ 127
short Short 2 字节 16 位 0 -32768 ~ 32767
int Integer 4 字节 32 位 0 -2^31 ~ 2^31 -1
long Long 8 字节 64 位 0 -2^63 ~ 2^63 -1
浮点型 float Float 4 字节 32 位 0.0f N/A
double Double 8 字节 64 位 0.0d N/A
  • int 和 Integer 的区别

    • int 是基本类型 32 位长度的整数。
    • Integer 是类类型,是 int 的封装类。
    • intInteger 之间可以通过 自动装箱 自动拆箱 互相转换。

2、属性

  • 属性
属性 简介
MAX_VALUE 最大值
MIN_VALUE 最小值
  • int 的最大值可以通过其对应的封装类 Integer.MAX_VALUE 获取。

    1
    2
    3
    4
    5
    // int 的 最大值
    Integer.MAX_VALUE

    // int 的 最小值
    Integer.MIN_VALUE

3、方法

  • 方法
方法 简介
xxxValue 将 Number 对象转换为 xxx 数据类型的值并返回。如:x.doubleValue,x 被转化为 double 类型
parseInt 用于将字符串参数作为有符号的十进制整数进行解析。
如果方法有两个参数,使用第二个参数指定的基数,将字符串参数解析为有符号的整数
toString 用于返回以一个字符串表示的 Number 对象值
equals 用于判断 Number 对象与方法的参数进是否相等,
如 Number 对象不为 Null,且与方法的参数类型与数值都相等返回 True,否则返回 False
compareTo 用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较,
如果指定的数与参数相等返回 0,如果指定的数小于参数返回 -1,如果指定的数大于参数返回 1
  • toString 返回以一个字符串表示的 Number 对象值。

    1
    2
    3
    4
    5
    6
    7
    Integer x = 12;

    // 返回 Integer 值的 String 对象
    String str = x.toString();

    // 返回指定 int 值的 String 对象
    String str = Integer.toString(12);

4、类型转换

  • 基本数字类型 转 数字封装类

    1
    2
    3
    4
    5
    6
    7
    int i = 5;

    // 方法 1:把一个基本类型的变量,转换为 Integer 对象
    Integer it = new Integer(i);

    // 方法 2:自动转换 装箱
    Integer it = i;
  • 数字封装类 转 基本数字类型

    1
    2
    3
    4
    5
    6
    7
    Integer it = new Integer(5);

    // 方法 1:把一个 Integer 对象,转换为一个基本类型的 int
    int i = it.intValue();

    // 方法 2:自动转换 拆箱
    int i = it;
  • 数字 转 字符串

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    int i = 5;

    // 方法 1:使用 String 类的静态方法 valueOf
    String str = String.valueOf(i);

    // 方法 2:先把基本类型装箱为对象,然后调用对象的 toString
    Integer it = i;
    String str2 = it.toString();

    // 方法 3
    String str3 = Integer.toString(i);
  • 字符串 转 数字

    1
    2
    3
    4
    String str = "999";

    // 调用 Integer 的静态方法 parseInt
    int i = Integer.parseInt(str);

5、Math 数学方法

  • java.lang.Math 提供了一些常用的数学运算方法,并且都是以静态方法的形式存在。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package java.lang;

    /**
    * The class {@code Math} contains methods for performing basic
    * numeric operations such as the elementary exponential, logarithm,
    * square root, and trigonometric functions.
    *
    * @author unascribed
    * @author Joseph D. Darcy
    * @since JDK1.0
    */

    public final class Math {

    }
  • 常用方法

方法 描述
Math.max(a, b) a 和 b 的最大值
Math.min(a, b) a 和 b 的最小值
Math.abs(a) 绝对值 a
Math.sqrt(a) a 的平方根
Math.pow(a, b) a 的 b 次方
Math.round(a) 最接近的整数
Math.sin(ang) 正弦
Math.cos(ang) ang 的余弦
Math.tan(ang) ang 的切线
Math.asin(ang) ang 的反正弦
Math.log(a) a 的自然对数
Math.toDegrees(rad) 角度 rad
Math.toRadians(deg) 以弧度为单位的角度度
Math.random() 随机数 [0, 1)
Math.PI π
Math.E 自然常数
  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    float f1 = 5.4f; 
    float f2 = 5.5f;

    // 四舍五入
    System.out.println(Math.round(f1));

    // 随机数
    System.out.println(Math.random()); // 0-1 之间的 随机浮点数(取不到 1)
    System.out.println((int)(Math.random() * 10)); // 0-10 之间的 随机整数(取不到 10)

    // 开方
    System.out.println(Math.sqrt(9));

    // 次方
    System.out.println(Math.pow(2, 4)); // 2 的 4 次方

    // π
    System.out.println(Math.PI);

    // 自然常数
    System.out.println(Math.E);

6、格式化输出

  • 如果不使用格式化输出符,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐,使用格式化输出,就可以简洁明了。
格式 说明
%s 表示字符串
%d 表示数字
%n 表示换行,就是另起一行,可以做到与平台无关的换行
%r 表示回车,就是回到一行的开头
%8d 总长度是 8,默认右对齐
%-8d 总长度是 8,左对齐
%08d 总长度是 8,不够补 0
%,d 千位分隔符
%.2f 小数点位数
Locale.US, "%,.2f %n" 不同国家的千位分隔符
  • 控制台输出函数
关键字 说明
println 会自动换行
printf 不会自动换行
format 不会自动换行,和 printf 能够达到一模一样的效果
  • 实例

    1
    2
    3
    4
    5
    6
    /* println */

    String name ="盖伦"; int kill = 8; String title="超神";

    // 直接使用 + 进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
    System.out.println(name + " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号");
    1
    2
    3
    4
    5
    6
    7
    /* printf */

    // 直接使用 + 进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
    System.out.printf(name + " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号");

    // 使用格式化输出,%s 表示字符串,%d 表示数字,%n 表示换行
    System.out.printf("%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n", name, kill, title);
    1
    2
    3
    4
    5
    6
    7
    /* format */

    // 直接使用 + 进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
    System.out.format(name + " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号");

    // 使用格式化输出,%s 表示字符串,%d 表示数字,%n 表示换行
    System.out.format("%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n", name, kill, title);
  • 换行符

    • 换行符就是另起一行 — \n 换行(newline)
    • 回车符就是回到一行的开头 — \r 回车(return)
    • 在 eclipse 里敲一个回车,实际上是回车换行符
    • Java 是跨平台的编程语言,同样的代码,可以在不同的平台使用,比如 Windows、Linux、Mac

    • 然而在不同的操作系统,换行符是不一样的

      • 在 DOS 和 Windows 中,每行结尾是 \r\n
      • Linux 系统里,每行结尾只有 \n
      • Mac 系统里,每行结尾是只有 \r
    • 为了使得同一个 Java 程序的换行符在所有的操作系统中都有一样的表现,使用 %n,就可以做到平台无关的换行。

  • 其他常用的格式化方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    int year = 2020;

    // 直接打印数字
    System.out.printf("%d %n", year);

    // 总长度是 8,默认右对齐
    System.out.printf("%8d %n", year);

    // 总长度是 8,左对齐
    System.out.printf("%-8d %n", year);

    // 总长度是 8,不够补 0
    System.out.printf("%08d %n", year);

    // 千位分隔符
    System.out.printf("%,d %n", year * 10000);

    // 小数点位数
    System.out.printf("%.2f %n", Math.PI);

    // 不同国家的千位分隔符
    System.out.printf(Locale.FRANCE, "%,.2f %n", Math.PI * 10000);
    System.out.printf(Locale.US, "%,.2f %n", Math.PI * 10000);
    System.out.printf(Locale.UK, "%,.2f %n", Math.PI * 10000);
文章目录
  1. 1. 前言
  2. 2. 1、数字类
  3. 3. 2、属性
  4. 4. 3、方法
  5. 5. 4、类型转换
  6. 6. 5、Math 数学方法
  7. 7. 6、格式化输出
隐藏目录