HomeJava

Java for Testers – Assigning out of range integer values to the byte, short and int data types

Java for Testers – Assigning out of range integer values to the byte, short and int data types

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

In the previous article, I have explained about assigning byte, short, long, float and boolean values to the int data type declared variables.

In this article, I am going to explain about assigning ‘out of range’ integer values to the byte, short & int data types.

On assigning the ‘out of range’ integer values, we will get compiler errors and have to rectify them by changing the data type of the declared variable.

Java for Testers – Assigning out of range integer values to the byte, short and int data types

Java will give a compile-time error (i.e. Error will be displayed before executing the program) on assigning the ‘out of range’ integer values to the byte, short and int data type variables.

Let me explain by assigning one after the other ‘out of range’ integer values to the data type variables:

Assigning ‘out of range’ integer values to the byte declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning ‘out of range’ value to the byte data type declared variable:

byte data type declared variable, accepts the integer values in the range of -128 to 127

On assigning the out of range value say 130 to the byte declared variable, we will get compiler error.

Let’s follow the below steps to find out in a practical way:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with byte data type as shown below:

out of range - byte

3) Assing the out of range integer value say 130, to the byte data type variable as shown below:

out of range - byte assign out of range

4) Hover the mouse on the compiler error and observe that the message ‘Type mismatch: cannot convert from int to byte’ as shown below:

out of range - byte cannot convert

5) To fix the above compiler error, we have to change the data type from byte to short and observe that the compiler error will be resolved as shown below:

out of range - byte to short

The above compiler error got resolved as the integer value range for the short data type is  -32,768 to 32,767.

Assigning ‘out of range’ integer values to the short declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning ‘out of range’ value to the short data type declared variable:

short data type declared variable, accepts the integer values in the range of -32,768 to 32,767

On assigning the out of range value say 33968 to the short declared variable, we will get compiler error.

Let’s follow the below steps to find out in a practical way:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

out of range - short

3) Assing the out of range integer value say 33968, to the short data type variable as shown below:

out of range - short cannot convert

4) Hover the mouse on the compiler error and observe that the message ‘Type mismatch: cannot convert from int to short’ as shown below:

out of range - hover cannot convert from int to short

5) To fix the above compiler error, we have to change the data type from short to int and observe that the compiler error will be resolved as shown below:

out of range - short to int coverted error resolved

 

The above compiler error got resolved as the integer value range for the int data type is – 2,147,483,648 to 2,147,483,647

Assigning ‘out of range’ integer values to the int declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning ‘out of range’ value to the int data type declared variable:

int data type declared variable, accepts the integer values in the range of – 2,147,483,648 to 2,147,483,647

On assigning the out of range value say 33911234567 to the int declared variable, we will get a compiler error.

Let’s follow the below steps to find out in a practical way:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with int data type as shown below:

out of range - int

3) Assing the out of range integer value say 33911234567, to the int data type variable as shown below:

out of range - int assigned

4) Hover the mouse on the compiler error and observe that the message ‘The literal 33911234567 of type int is out of range’ as shown below:

out of range - int assigned compiler error

To fix the above compiler error, we have to first change the data type from int to long and observe that the compiler error will still be there as shown below:

out of range - still out of range

Now, add the letter L at the end of the integer value to make it a long type value and observe the compiler error will  be resolved as shown below:

out of range - resolved

The above error got resolved as the long type value range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and these values should have L added at the end of the value.

Here conclude this article on arranging out or range values to the byte, short and int data type variables and the ways of fixing them.

In the next article, I will explain the remaining things about data types.

Next Steps:

  • > To learn more about Java, continue to the next post (Click on Next Post link below)
  • > Check complete Java Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below:

Happy Learning ?

Arun Motoori (www.QAFox.com)

On a mission to help the Testing Community in all possible ways.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp