HomeJava

Java for Testers – Assigning byte, short, long, float and boolean values to the int data type declared variable

Java for Testers – Assigning byte, short, long, float and boolean values to the int data type declared variable

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

In the previous articles, I have explained about assigning the character values to the int data type declared variable.

In this article, I am going to explain and show you what happens when we try to assign the byte, short, long, float and boolean values to the int declared variable.

Java for Testers – Assigning byte, short, long, float and boolean values to the int data type declared variable

Java will give a compile-time error (i.e. Error will be displayed before executing the program) on assigning the long, float and boolean values to the int data type declared variable.

Java will not give compile-time error on assigning the byte and short values to the into the int data type declared variables.

Let me explain by assigning one after the other type values to the int data type variable:

Assigning byte value to the int data type declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning any byte value to the int data type declared variable:

Example of byte values: byte values are similar to integer values, but in the range of -128 to 127

I will demonstrate assigning byte values to the int data type variable by taking a value say 123 which is in the range of byte.

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:

decimal to int - declaring int

3)  Assing the byte value say 123 to the above int data type declare variable as shown below:

all types to int - byte

4) Now print the byte value assigned to the int data type variable by printing the variable using the print statement as shown below:

all types to int - byte print

5) Save the Project and click on the ‘Run’ button.

Observe that the Program got executed and the byte value got printed as shown below:

all types to int - byte printed

Copy the above-explained code from the below section

Assigning short value to the int data type declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning any short value to the int data type declared variable:

Example of short values: short values are similar to integer values, but in the range of -32,768 to 32,767

I will demonstrate assigning short values to the int data type variable by taking a value say 32123 which is in the range of short.

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:

decimal to int - declaring int

3)  Assing the byte value say 32123 to the above int data type declare variable as shown below:

all types to int - short

4) Now print the short value assigned to the int data type variable by printing the variable using the print statement as shown below:

all types to int - short print

5) Save the Project and click on the ‘Run’ button.

Observe that the Program got executed and the short value got printed as shown below:

all types to int - short printed

Copy the above-explained code from the below section

Assigning long value to the int data type declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning any long value to the int data type declared variable:

Example of long values: long values looks like integer values but have L letter at the end of the value as 1234L

I will demonstrate assigning long values to the int data type variable by taking a value say 1234L

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:

decimal to int - declaring int

3)  Assing the byte value say 1234L to the above int data type declared variable as shown below:

all types to int - long

4) Hover the mouse on the above-displayed compile-time error and observe that the compile-time error ‘Type Mismatch – cannot convert from long to int’ will be displayed as shown below:

all types to int - long compiler error

Hence we cannot assign the long type values to the int data type variables.

Assigning float value to the int data type declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning any float value to the int data type declared variable:

Example of float values: float values looks like decimal values but have f letter at the end of the value as 3.69L

I will demonstrate assigning float values to the int data type variable by taking a value say 1234L

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:

decimal to int - declaring int

3)  Assing the float value say 3.69f to the above int data type declared variable as shown below:

all types to int - float

4) Hover the mouse on the above-displayed compile-time error and observe that the compile-time error ‘Type Mismatch – cannot convert from float to int’ will be displayed as shown below:

all types to int - float compiler error

Hence we cannot assign the float type values to the int data type variables.

Assigning boolean value to the int data type declared variable

Follow the below steps to find out whether the compiler error is displayed on assigning any boolean value to the int data type declared variable:

Example of boolean values: true and false are the only two boolean values available.

I will demonstrate assigning boolean values to the int data type variable by taking a value say false

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:

decimal to int - declaring int

3)  Assing the boolean value say false to the above int data type declared variable as shown below:

all types to int - boolean

4) Hover the mouse on the above-displayed compile-time error and observe that the compile-time error ‘Type Mismatch – cannot convert from booelan to int’ will be displayed as shown below:

all types to int - boolean compiler error

Hence we cannot assign the boolean type values to the int data type variables.

Here concludes this article.

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>>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

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