<<Previous Post << Complete Tutorial>> Next Post>>
In the previous article, I have explained about short data type.
In this article, I am going to explain and practically demonstrate long data type.
Java for Testers – long data type
long data type also stores the integer values similar to int data type.
But the size of long data type is 2 times larger than the int data type.
i.e. int data type is 32 bit and can store the values in the range of -2147483648 to 2147483647
Whereas long is 32 bit and can store the values in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
In general, we use int data type for storing the integer values into a variable, but we may have to use long data type when you get any large size number which you are unable to store into the variable.
Also, we have to add the letter L at the end of the value to make it a long type integer literal.
Example of using long data type:
long h = 9L;
In the above example, we have declared the variable h with the long data type, and hence we are able to store the long type integer values in the range of -32,768 to 32,767 into the variable.
Also, observe that we have added a letter L to the value 9 to from long type integer literal value 9L.
Follow the below steps for practically implementing short 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
2) Inside the main() method, let’s create a variable, declare the variable with long data type and assign the variable with an integer value in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 say 9 as shown below:
3) Now print the value stored in the variable by printing the variable using the print statement as shown below:
4) Save the Project and click on the ‘Run’ button.
Observe that the Program got executed and the long type integer value stored in the long declared variable h got printed as shown below:
Copy the above-explained code from the below section:
public class Demo { public static void main(String[] args) { long h = 9L; System.out.println(h); } }
Here concludes this article.
In the next article, I will explain and demonstrate the float 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>>