HomeJava

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

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

<<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 float data type variable.

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

Assigning integer literals to the float data type variable

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

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

Example: If you assign the integer value say 123 to the float type declared variable and then print the variable, it will print the floating-point 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 float data type as shown below:

float assigned different - float

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

float assigned different - float assign integer

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

float assigned different - float assign integer printed

Assigning long literals to the float data type variable

float 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 float data type variable, will be converted from the long type value to the floating-point value by Java before assigning.

Example: If you assign the long literal value say 97612345L to the float type declared variable and then print the variable, it will print the floating-point 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 float data type as shown below:

float assigned different - float

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

float assigned different - float assign long

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

float assigned different - float assign long printed

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

Assigning character literal to the float data type variable

On assigning the character type literal to the float 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 floating-point 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 float data type as shown below:

float assigned different - float

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

float assigned different - float assign character

4) Now print the value stored in the float 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 floating-point value and then assigned to the variable and then printed as shown below:

float assigned different - float assign character printed

Assigning floating-point literal to the float data type variable

float 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 float data type variable, will be assigned directly without any conversions and errors.

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

float assigned different - float

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

float assigned different - float assign float

4) Now print the value stored in the float declared variable using the print statement and observe that the assigned floating-point literal value will be printed as it is as shown below:

float assigned different - float assign float printed

Assigning double, boolean and String literals to the float 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 double, boolean and String literals to the float data type variable:

float assigned different - decimal error

float assigned different - boolean error

float assigned different - string error

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

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 *