当深入研究 java 的数据类型时,了解 float 和 double 至关重要。这些是用于实数的浮点数据类型,可以精确处理微小和大量的值。
public class floatexample {
public static void main(string[] args) {
float numa = -101.23f;
float numb = 2.356f;
system.out.println(numa + numb); // output: -98.874
}
}
public class DoubleExample {
public static void main(String[] args) {
double valueOne = 0.5; // Implicitly a double
double valueTwo = 0.5d; // Explicitly a double
double valueThree = 0.123456789; // High precision
System.out.println(valueOne); // Output: 0.5
System.out.println(valueTwo); // Output: 0.5
System.out.println(valueThree); // Output: 0.123456789
}
}