HomeJava

Java for Testers – int data type

Java for Testers – int data type

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

In the previous article, I explained different types of data types in Java

In this article, I am going to explain and practically demonstrate the int primitive data type in Java.

Java for Testers – int data type

Integer values like 9, 33, 69, etc. can be stored into a variable which is declared with int data type.

For example:

In the above example, we have declared the variable a with int data type, and hence we are able to store the integer values like 9 into the variable.

Follow the below steps for practically implementing int data type in Java:

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, declare the variable with int data type and assign the variable with an integer value 9 as shown below:

Using Data Types Java - int

3) Now print the value stored in the variable by printing the variable using the print statement as shown below:

Using Data Types Java - print int

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

Observe that the Program got executed and the value 9 stored in the variable a got printed as shown below:

Using Data Types Java - int printed

Copy the above-explained code from the below section:


Before concluding this article, I would like to give more details about the int data type in Java.

int is a 32-bit size data type which can store the integer values in the range of – 2,147,483,648 to 2,147,483,647 (i.e. -2 ^ 31 to 2 ^ 31 -1 )

The default value of an integer variable is 0.

There are other data types in Java which store the integer values i.e. byte, short and long.

The following is the difference between int, byte, short and long data types:

Data Type Size Range of values
int 32 bit – 2,147,483,648 to 2,147,483,647
byte 8 bit -128 tp -127
short 16 bit -32,768 to 32,767
long 64 bit 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Though there are different data types in Java which can store integer values, we generally use int data type for storing integer values unless there are memory problems while using them.

I will explain and demonstrate the other integer value storing data types i.e. byte, short and long in the upcoming articles.

Here concludes this article.

In the next article, I will explain and demonstrate the double data type in Java.

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 *