<<Previous Post << Complete Tutorial>> Next Post>>
In the previous article, I have explained assigning different types of literals to the long data type variable.
In this article, I am going to explain assigning different types of literals to the long data type variable.
Java for Testers – Assigning different types of literals to the long data type variable
There are different types of literals like integer, long, character, boolean, floating-point, double and string literals.
I will practically demonstrate assigning the literals one after the other to the long type declared variable.
Assigning Integer literals in the range of -128 to 127 (i.e. byte data type range) to the long data type variable
Let’s start by assigning the integer literals in the range of -128 to 127 (i.e. byte data type range) to the long data type variable.
For example, let’s assign the integer value 115 (i.e. within the range of byte data type) to the long data type variable.
Follow the below steps for implementing this:
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 and declare it with long data type as shown below:
3) Assing the within byte date type range integer value say 115, to the long data type variable as shown below:
In the above example, there is no compiler error and the within range byte data type integer value 115 got assigned properly to the long type declared variable.
Assigning Integer literals in the range of -32,768 to 32,767 (i.e. short data type range) to the long data type variable
Let’s start by assigning the integer literals in the range of -32,768 to 32,767 (i.e. short data type range) to the long data type variable.
For example, let’s assign the integer value 12356 (i.e. within the range of short data type) to the long data type variable.
Follow the below steps for implementing this:
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 and declare it with long data type as shown below:
3) Assing the within short date type range integer value say 12356 , to the long data type variable as shown below:
In the above example, there is no compiler error and the within range short data type integer value 12356 got assigned properly to the long type declared variable.
Assigning Integer literals in the range of -2,147,483,648 to 2,147,483,647 (i.e. int data type range) to the long data type variable
Let’s start by assigning the integer literals in the range of – 2,147,483,648 to 2,147,483,647 (i.e. int data type range) to the long data type variable.
For example, let’s assign the integer value 1,147,483,648 (i.e. within the range of int data type) to the long data type variable.
Follow the below steps for implementing this:
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 and declare it with long data type as shown below:
3) Assing the within int date type range integer value say 114748364, to the long data type variable as shown below:
In the above example, there is no compiler error and the within range int data type integer value 114748364 got assigned properly to the long type declared variable.
Assigning out of range Integer literals (i.e. the number greater than 2,147,483,647 or less than – 2,147,483,648) to the long data type variable
Let’s start by assigning the numerical literal which is out of the range of – 2,147,483,648 to 2,147,483,647 (i.e. out of int data type range say 3,147,483,647 ) to the long data type variable.
For example, let’s assign the out of range integer value 3,147,483,647 (i.e. out of the range of int data type) to the long data type variable.
Follow the below steps for implementing this:
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 and declare it with long data type as shown below:
3) Assing the out of range of int date type integer value say 3147483647, to the long data type variable and observe that the compiler error – “The literal 3147483647 of type int is out of range’ will be displayed as shown below:
In the above example, the compiler error is displayed as the numerical value assigned to the long data type variable is an integer value and is out of the largest int data type range (i.e. more than 2,147,483,647).
In order to fix the above compiler error, we have to change the literal type to long literal type which can accept the numerical values in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
The only way to convert the above assigned numerical value 3147483647 is to add the letter L at the end of the value to become 3147483647L.
Assing the long type literal value 3147483647L to the long data type variable and observe that the compiler error will be resolved as shown below:
Assigning character literal to the long data type variable
On assigning the character type literal to the long data type variable, we will not get a compiler error. Instead, the ASCII value of character liter ‘p’ i.e. ( 115 as shown at this page) will be assigned.
character type literals are the single letter characters within the single quotes. For example ‘p’ is the character literal.
Follow the below steps for implementing this:
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 and declare it with long data type as shown below:
3) Assing the character literal value say ‘p’, to the long data type variable as shown below:
4) Now print the value stored in the long declared variable using the print statement and observe that the ASCII value of the assigned character will be converted, assigned and then printed as shown below:
Assigning other literal type like float, double, boolean and String to the long data type variable will give compiler errors as shown in the below screenshots:
Here conclude this article on assigning the different types of Literals to the long data type variable in Java.
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>>