Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - get int max value

0 contributions
8 points

Short solution:

int MAX_VALUE = Integer.MAX_VALUE;

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

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

Short solution to get int min value:

int MIN_VALUE = Integer.MIN_VALUE;

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

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

1. Different ways of how to get Integer max value

Below we can see 4 different ways of how to get Integer max value in java. 

public class Example1 {

    public static void main(String[] args) {

        int MAX_VALUE = Integer.MAX_VALUE;
        System.out.println(MAX_VALUE);   // 2147483647

        int MAX_VALUE_2 = (int) Math.pow(2, 31);
        System.out.println(MAX_VALUE_2); // 2147483647

        int MAX_VALUE_3 = -1 >>> 1;
        System.out.println(MAX_VALUE_3); // 2147483647

        int MAX_VALUE_4 = ~0 >>> 1;
        System.out.println(MAX_VALUE_4); // 2147483647
    }
}

Output:

2147483647
2147483647
2147483647
2147483647

2. Integer overflow example

public class Example2 {

    public static void main(String[] args) {

        int MAX_VALUE = Integer.MAX_VALUE;
        int overflow = MAX_VALUE + 100;
        System.out.println(overflow); // -2147483549
    }
}

Output:

-2147483549

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more