难以理解 java.lang.Math 输出

Difficulties understanding java.lang.Math output

我很难理解为什么输出不同。希望有人可以解释这里发生了什么。

当我输入 2 时,下面的代码按预期计算体积和表面积(分别为 33.5103 和 50.2655),但是如果我将体积方程从 (4.0 / 3) * Math.PI * Math.pow(r, 3); 更改为 (4 / 3) * Math.PI * Math.pow(r, 3);, 输出为 25.1327

为什么我把4.0改成4输出结果不一样?

import java.lang.Math;  // Import the Math class.
import java.util.Scanner;  // Import the Scanner class.
import java.text.DecimalFormat; // Import the DecimalFormat class.

class Main {

  public static double calcVolume(double r) {
    // Return Volume.
    return (4.0 / 3) * Math.PI * Math.pow(r, 3);
  }

  public static double calcSurfaceArea(double r) {
    // Return the Surface Area.
    return 4 * Math.PI * Math.pow(r, 2);
  }

  public static void main(String[] args) {

    // Prompt user for the radius.
    Scanner radius_in = new Scanner(System.in);
    System.out.println("Please enter the radius to calculate volume and surface area: "); 

    Double radius = radius_in.nextDouble();

    Double volume = calcVolume(radius);
    Double surfaceArea = calcSurfaceArea(radius);

    // Set up decimalformat object for outputting to 4 decimal places.
    DecimalFormat DF = new DecimalFormat("0.####");

    System.out.println("Volume is: " + DF.format(volume));
    System.out.println("Surface Area is: " + DF.format(surfaceArea));

  }
}

这是因为 4.0 / 3 你得到的是浮点精度,而 4 / 3 被认为是 int。这意味着,无论评估为 4 / 3,小数部分都会被截断:

4.0 / 3 => 1.(3)
4 / 3 => (int) (1.(3)) => 1