Verify Integer Against Short Limits
In the previous step, we saw how to declare and use short
variables within their valid range. Now, let's explore what happens when we try to assign a value that is outside the short
range to a short
variable.
Java has different integer data types, including byte
, short
, int
, and long
, each with a different range. An int
(integer) is a 32-bit signed two's complement integer, with a range from -2,147,483,648 to 2,147,483,647. This range is much larger than that of a short
.
When you try to assign a value from a larger data type (like int
) to a smaller data type (like short
), Java requires you to explicitly tell it that you are aware of the potential loss of information. This is called type casting. If you don't cast, Java will give you a compilation error to prevent accidental data loss.
Let's modify our HelloJava.java
program to demonstrate this.
-
Open the HelloJava.java
file in the WebIDE editor.
-
Replace the existing code with the following:
public class HelloJava {
public static void main(String[] args) {
int largeInt = 40000; // This value is outside the short range
// Attempt to assign largeInt to a short variable without casting
// short myShort = largeInt; // This line will cause a compilation error
// To assign a value from a larger type, we need to cast it
short myShort = (short) largeInt;
System.out.println("Value of largeInt cast to short: " + myShort);
int anotherLargeInt = -40000; // Another value outside the short range
short anotherShort = (short) anotherLargeInt;
System.out.println("Value of anotherLargeInt cast to short: " + anotherShort);
}
}
In this updated code:
- We declare an
int
variable largeInt
with the value 40000, which is greater than the maximum value a short
can hold (32767).
- The commented-out line
short myShort = largeInt;
shows what would happen without casting โ a compilation error.
- The line
short myShort = (short) largeInt;
demonstrates how to cast the int
value to a short
. The (short)
before largeInt
is the cast operator.
- We do the same for a negative value
anotherLargeInt
(-40000), which is less than the minimum value a short
can hold (-32768).
-
Save the file (Ctrl+S or Cmd+S).
-
Now, compile the modified program in the Terminal:
javac HelloJava.java
This time, the compilation should be successful because we used type casting.
-
Run the compiled program:
java HelloJava
You will see output similar to this:
Value of largeInt cast to short: -25536
Value of anotherLargeInt cast to short: 25536
Notice that the output values (-25536 and 25536) are not the original values (40000 and -40000). This is because when you cast a value that is outside the range of the target data type, the value "wraps around". This is an example of overflow (for positive values exceeding the max) and underflow (for negative values exceeding the min). The exact resulting value depends on how the numbers are represented in binary. This demonstrates why understanding data type ranges and using casting carefully is important to avoid unexpected results.