Languages
[Edit]
EN

Java - get int min value

10 points
Created by:
AnnLen
1777

Short solution:

int MIN_VALUE = Integer.MIN_VALUE;

System.out.println(MIN_VALUE); // -2147483648

Java Integer min value is -2,147,483,648.

Short solution to get int max value:

int MAX_VALUE = Integer.MAX_VALUE;

System.out.println(MAX_VALUE); // 2147483647

Java Integer max value is 2,147,483,647.

1. Different ways of how to get Integer min value

public class Example1 {

    public static void main(String[] args) {

        int MIN_VALUE = Integer.MIN_VALUE;
        System.out.println(MIN_VALUE);   // -2147483648

        int MIN_VALUE_2 = (int) Math.pow(2, 31) + 1;
        System.out.println(MIN_VALUE_2); // -2147483648

        int MIN_VALUE_3 = -1 << 31;
        System.out.println(MIN_VALUE_3); // -2147483648

        int MIN_VALUE_4 = ~(~0 >>> 1);
        System.out.println(MIN_VALUE_4); // -2147483648
    }
}

Output:

-2147483648
-2147483648
-2147483648
-2147483648

2. Integer overflow example

public class Example2 {

    public static void main(String[] args) {

        int MIN_VALUE = Integer.MIN_VALUE;
        int overflow = MIN_VALUE - 100;
        System.out.println(overflow); // 2147483548
    }
}

Output:

2147483548

 

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.