Junior — Middle
How can you determine the maximum possible number stored in an int data type?
sobes.tech AI
Answer from AI
In Java, the maximum value for the int type can be determined in several ways:
- Using the constant from the wrapper class
Integer:
int maxInt = Integer.MAX_VALUE;
System.out.println(maxInt); // 2147483647
- Calculating manually, considering that
intis a 32-bit signed type, the maximum value is 2^31 - 1:
int maxInt = (int) Math.pow(2, 31) - 1;
System.out.println(maxInt); // 2147483647
- Using bitwise operations:
int maxInt = (1 << 31) - 1; // shift 1 left by 31 bits, minus 1
System.out.println(maxInt); // 2147483647
The first method is the simplest and most reliable, as it uses the standard constant.