Java学习10.8(动手动脑)

请看以下代码,你发现了有什么特殊之处吗?

//MethodOverload.java
//Using overloaded methods

public class MethodOverload {

    public static void main(String[] args) 
    {
        System.out.println("The square of integer 7 is " + square(7));
        System.out.println("
The square of double 7.5 is " + square(7.5));
    }

    public static int square(int x) 
    {
        return x * x;
    }

    public static double square(double y) 
    {
        return y * y;
    }
}

 特殊之处:整数类型的乘法仍然是整型,java里面double和float类型是非精确数字类型,存在一定的误差,误差值具有随机性,如果要精确计算的,但精确度要求不大的数据可以使用double或float类型。

原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/13799101.html