HomeJava

Java for Testers – Passing decimal values to the int data type declared variables

Java for Testers – Passing decimal values to the int data type declared variables

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

In the previous articles, I have explained about different data types in Java.

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

Java for Testers – Passing decimal values to the int data type declared variables

Java will give compile-time error (i.e. Error will be displayed before executing the program) on assigning the decimal value to the int data type declared variable.

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

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 decimal value say 3.69 to the above int data type declare variable as shown below:

decimal to int - assign decimal

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

decimal to int - cannot convert

Hence it is clear that we cannot assign the decimal value to the int declared variable and on trying to do so we get a compiler error before execution which says ‘cannot convert from double to int’.

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 *