HomeJava

Java for Testers – Assigning different types of literals to the double data type variable

Java for Testers – Assigning different types of literals to the double data type variable

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

In the previous article, I have explained about assigning different types of literals to the float data type variable.

In this article, I am going to explain about assigning different types of literals to the double data type variable.

Java for Testers – Assigning different types of literals to the double 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 double data type declared variable.

Assigning integer literals to the double data type variable

double data type variable can be assigned with any integer values and it won’t result in an error.

Integer values when assigned to the double data type variable, will be converted from the integer value to the double value by Java before assigning.

Example: If you assign the integer value say 123 to the double type declared variable and then print the variable, it will print the double value (i.e. 123.0) that is converted from the assigned integer value (i.e. 123).

Follow the below steps for implementing the above example:

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 double data type as shown below:

double assigned differently - double

 

3) Assing the integer value say 123, to the double data type variable as shown below:

double assigned differently - double integer

4) Now print the value stored in the double declared variable using the print statement and observe that the integer value (i.e. 123) assigned to the variable will be converted to double decimal value (i.e. 123.0), and then assigned &  printed as shown below:

double assigned differently - double integer printed

Assigning long literals to the double data type variable

double data type variable can be assigned with any long literal values and it won’t result in an error.

long literals have the L letter at the end of their numerical value (Example: 97612345L)

Long literal values when assigned to the double data type variable, will be converted from the long type value to the double value by Java before assigning.

Example: If you assign the long literal value say 97612345L to the double type declared variable and then print the variable, it will print the double value (i.e. 9.7612344E7) that is converted from the assigned long literal value (i.e. 97612345).

Follow the below steps for implementing the above example:

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 double data type as shown below:

double assigned differently - double

3) Assing the long literal value say 97612345L, to the double data type variable as shown below:

double assigned differently - double long

4) Now print the value stored in the double declared variable using the print statement and observe that the long literal value (i.e. 97612345L) assigned to the variable will be converted to double value (i.e. 9.7612344E7), and then assigned &  printed as shown below:

double assigned differently - double long printed

In the above output, the double value got printed in a scientific way (For more information on this, refer our article on scientific representation)

Assigning character literal to the double data type variable

On assigning the character type literal to the double 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 converted to the double value and then assigned to the variable.

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

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with double data type as shown below:

double assigned differently - double

3) Assing the character literal value say ‘p’, to the double data type variable as shown below:

double assigned differently - double character

4) Now print the value stored in the double declared variable using the print statement and observe that the ASCII value of character liter ‘p’ i.e. ( 115 as shown at this page) will be converted to the double decimal value and then assigned to the variable and then printed as shown below:

double assigned differently - double character printed

Assigning floating-point literal to the double data type variable

double data type variable can be assigned with floating-point literal values.

floating-point literals have the F letter at the end of their numerical decimal value (Example: 3.96F)

Floating-point literal values when assigned to the double data type variable, will be assigned to the variable after converting to the double type.

Follow the below steps for implementing the above example:

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 double data type as shown below:

double assigned differently - double

3) Assing the floating-point literal value say 3.96f, to the double data type variable as shown below:

double assigned differently - double float

4) Now print the value stored in the double declared variable using the print statement and observe that the assigned floating-point literal value will be converted to the double type, assigned and then printed as shown below:

double assigned differently - double float printed

Assigning boolean and String literals to the double data type variable will result in the below compiler errors.

The below screenshots will show you the compiler errors which got resulted from assigning the  boolean and String literals to the double data type variable:

double assigned differently - double boolean

double assigned differently - double string

Here conclude this article on assigning the different types of Literals to the double 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>>

Comments (0)

Leave a Reply

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

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp