1
public class Min_Max{
    public static void main(String[] args){
        //byte max & min
        // byte maxByte=Byte.MAX_VALUE;
        // byte maxByte_add1=Byte.MAX_VALUE+1;//Cannot convert from int to byte
        // byte minByte=Byte.MIN_VALUE;
        // byte minByte_sub1=Byte.MIN_VALUE-1;//Cannot convert from int to byte
        // //short max & min
        // short maxShort=Short.MAX_VALUE;
        // short maxShort_add1=Short.MAX_VALUE+1;//Cannot convert from int to short
        // short minShort=Short.MIN_VALUE;
        // short minShort_sub1=Short.MIN_VALUE-1;//Cannot convert from int to short
        //integer max & min
        int maxInt=Integer.MAX_VALUE;
        System.out.println("Integer max value :"+maxInt);
        int maxInt_add1=Integer.MAX_VALUE+1;
        System.out.println("Max+1 :"+maxInt_add1);
        int minInt=Integer.MIN_VALUE;
        System.out.println("Integer min value :"+minInt);
        int minInt_sub1=Integer.MIN_VALUE-1;
        System.out.println("Min-1 :"+minInt_sub1);
        //float max & min
        float maxFloat=Float.MAX_VALUE;
        System.out.println("Float max value :"+maxFloat);
        float maxFloat_add1=Float.MAX_VALUE+1;
        System.out.println("Max+1 :"+maxFloat_add1);
        float minFloat=Float.MIN_VALUE;
        System.out.println("Float min value :"+minFloat);
        float minFloat_sub1=Float.MIN_VALUE-1;
        System.out.println("Min-1 :"+minFloat_sub1);
        //double max & min
        double maxDouble=Double.MAX_VALUE;
        System.out.println("Double Max value :"+maxDouble);
        double maxDouble_add1=Double.MAX_VALUE+1;
        System.out.println("Max+1 :"+maxDouble_add1);
        double minDouble=Double.MIN_VALUE;
        System.out.println("Double Min value :"+minDouble);
        double minDouble_sub1=Double.MIN_VALUE-1;
        System.out.println("Min-1 :"+minDouble_sub1);
    }
}

![OUTPUT] For byte & short=>Cannot convert from int to byte or short. int=>Max value=min value-1 and Min value=Max value+1 float & double=>Max value=Max value+1 and no order for min values.

Can anyone explain how do these data types work for min-1 & max+1 values and why act in different ways?

1 Answers1

1

The lines that don't compile don't compile because + and - are two of those operators that cause their operands to undergo binary numeric promotion. The rules are:

  • If either operand is of type double, the other is converted to double.

  • Otherwise, if either operand is of type float, the other is converted to float.

  • Otherwise, if either operand is of type long, the other is converted to long.

  • Otherwise, both operands are converted to type int.

So if none of the operands are double, float or long, then both operands are converted to int, as is the case with your code. And int can't be implciitly converted to byte or short.

The max value of int is equal to the min value of int minus one because that's how two's complement works. The binary representation of the max value is:

0111 1111 1111 1111 1111 1111 1111 1111

Whereas the binary representation of the min value is:

1000 0000 0000 0000 0000 0000 0000 0000

You can now see how adding one to the max value gives you the min value, and subtracting one from the min value gives you the max value.

For float and double, the reason for max value = max value + 1 is basically that they are not precise enough to represent the value of max value + 1. See Is floating point math broken? The result is rounded to the nearest representable value, which just so happens to be the max value. A more precise description is found in the language spec:

If the magnitude of the sum is too large to represent, we say the operation overflows; the result is then an infinity of appropriate sign.

Otherwise, the sum is rounded to the nearest value in the chosen value set using IEEE 754 round-to-nearest mode. The Java programming language requires support of gradual underflow as defined by IEEE 754 (ยง4.2.4).

For why min value - 1 is -1.0, well, have a look at what the min value actually is. For float, It is 1.4E-45, a positive number that is very close to 0. Well what happens when you subtract 1 from a number that is slightly more than 0? You get approximately -1! Again, float and double can't represent this nearly-negative-one number precisely, so it shows -1.0.

You might have misunderstood Float.MIN_VALUE as the most negative value that Float can have, but that's not what it is about. The most negative value that Float can have is -Float.MAX_VALUE, or Float.NEGATIVE_INFINITY, depending on whether you count infinity as a "number" or not.

Sweeper
  • 213,210
  • 22
  • 193
  • 313