深入学习java源码之Math.max()与 Math.min()

深入学习java源码之Math.max()与 Math.min()

java基本数据类型及自动转型

8种基本数据类型及其所占空间大小:

一、byte,占用一个字节,取值范围为 -128-127,默认是“\u0000”,表示空
二、short,占用两个字节,取值范围为 -32768-32767
三、int,占用四个字节,-2147483648-2147483647
四、long,占用八个字节,对 long 型变量赋值时必须加上"L"或“l”,否则不认为是 long 型
五、float,占用四个字节,对 float 型进行赋值的时候必须加上“F”或“f”,如果不加,会产生编译错误,因为系统
自动将其定义为 double 型变量。double转换为float类型数据会损失精度。float a = 12.23产生编译错误的,float a = 12是正确的
六、double,占用八个字节,对 double 型变量赋值的时候最好加上“D”或“d”,但加不加不是硬性规定
七、char,占用两个字节,在定义字符型变量时,要用单引号括起来
八、boolean,只有两个值“true”和“false”,默认值为false,不能用0或非0来代替,这点和C语言不同

自动类型转换

1)两种类型是彼此兼容的

2)转换的目的类型占得空间范围一定要大于转化的源类型

正向过程:由低字节向高字节自动转换

byte->short->int->long->float->double

逆向过程:使用强制转换,可能丢失精度。

// 自动类型转换
short s=1;
int i;
// 自动类型转换 short类型转成int类型
i=s;

整数类型(byte/short/int/long)中,对于未声明数据类型的整形,其默认类型为int型。在浮点类型(float/double)中,对于未声明数据类型的浮点型,默认为double型。

int a=(int)3.14;

 小数的默认数字类型是double, 例如3.12. 当float a = 3.12时会报错, 因为3.12的默认数据类型是double, 我们需要使用如下的赋值方法:

float a = 3.12F
float b = (float)3.12

第一种方法在3.12后面加了一个F, 告诉编译器这是一个float的数. 第二种方法对3.12进行了强制的类型转换. 

double d=1.333;
float f;
// 把double类型的数据强制转换成float类型
f=(float)d;
int x;
double y;
x = (int)34.56 + (int)11.2;  // 丢失精度
y = (double)x + (double)10 + 1;  // 提高精度
System.out.println("x=" + x);
System.out.println("y=" + y);

x=45
y=56.0
Modifier and Type Method and Description
static double max(double a, double b)

返回两个 double值中的较大值。

static float max(float a, float b)

返回两个 float的较大值。

static int max(int a, int b)

返回两个 int值中的较大值。

static long max(long a, long b)

返回两个 long的较大值。

static double min(double a, double b)

返回两个 double的较小值。

static float min(float a, float b)

返回两个 float的较小值。

static int min(int a, int b)

返回两个 int的较小值。

static long min(long a, long b)

返回两个 long的较小值。

java源码

public final class Math {

    private Math() {}

    public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }

    public static long max(long a, long b) {
        return (a >= b) ? a : b;
    }
    //在有保证的非NaN参数上使用原始的逐位转换。
    private static long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
    private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);

    public static float max(float a, float b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0f) &&
            (b == 0.0f) &&
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a >= b) ? a : b;
    }

    public static double max(double a, double b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0d) &&
            (b == 0.0d) &&
            (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a >= b) ? a : b;
    }

    public static int min(int a, int b) {
        return (a <= b) ? a : b;
    }

    public static long min(long a, long b) {
        return (a <= b) ? a : b;
    }

    public static float min(float a, float b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0f) &&
            (b == 0.0f) &&
            (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a <= b) ? a : b;
    }

    public static double min(double a, double b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0d) &&
            (b == 0.0d) &&
            (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a <= b) ? a : b;
    }	

}
public final class StrictMath {

    private StrictMath() {}

    public static int max(int a, int b) {
        return Math.max(a, b);
    }

    public static long max(long a, long b) {
        return Math.max(a, b);
    }

    public static float max(float a, float b) {
        return Math.max(a, b);
    }	
   
    public static double max(double a, double b) {
        return Math.max(a, b);
    }
	
    public static int min(int a, int b) {
        return Math.min(a, b);
    }

    public static long min(long a, long b) {
        return Math.min(a, b);
    }

    public static float min(float a, float b) {
        return Math.min(a, b);
    }

    public static double min(double a, double b) {
        return Math.min(a, b);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/85828808
今日推荐