In this tutorial, we will discuss Java float and the floating-point types with details like width, range, size, and the usage example:
Even though float in Java is a simple concept, we have included all the necessary examples and programs that will be sufficient to make you understand the tutorial in detail.
What You Will Learn: [hide]
- Floating-Point Types
- Java Float
- Java Float Example
- Frequently Asked Questions
- Recommended Reading
Floating-point numbers are those numbers that require “fractional precision” i.e. the numbers that can be in the fraction.
There are a lot of mathematical calculations where we can use Floating-Point types like finding square root or cube root of any number, finding roots of quadratic equation, dealing with trigonometries like sin and cos, and so on.
There are two types of Floating-point types:
Enlisted below are the details about float and double type. The range is approximate. As you can clearly see, the float is smaller and has a lesser range than Java double.
In this tutorial, we will discuss the Float data type in detail.
|float||32||1.4e–045 to 3.4e+038|
|double||64||4.9e–324 to 1.8e+308|
Float is a single-precision value that has a width of 32 bits in storage. On some processors, this single precision is faster and takes less size when compared to the double-precision. This is arguable as on some modern processors, double-precision is faster than the single-precision.
As far as Java variables are concerned, we can use float while initializing or declaring any variable that may expect the output to be fractional.
// declaring temperature in Degree and Fahrenheit float temp_degree; Float temp_fahrenheit;
Java Float Example
In this example, we have initialized two float variables n1 and n2 with some value. Then, we have declared another float variable n3 that will contain the result of n1 multiplied with n2.
Thereafter, we have calculated the n1*n2 and stored it in n3 and finally printed the value of n3.
Frequently Asked Questions
Q #1) What is the default value and size of a float in Java?
Answer: The default value is 0.0f and the default size is 4 bytes of a float in Java.
Q #2) What is the difference between float and double in Java?
Answer: Enlisted below are the differences between float and double.
|It has an approximate range from 1.4e–045 to 3.4e+038.||It has an approximate range from 4.9e–324 to 1.8e+308.|
|Its width is 32 bit.||Its width is 64 bit.|
|The default size is 4 bytes.||The default size is 8 bytes.|
|The default value is 0.0f||The default value is 0.0d|
|It is a single-precision value.||It is a double-precision value.|
Q #3) Can we assign a decimal value in Java float?
Answer: No. Given below is an example where we have assigned a decimal value in a float that will throw an error.
However, we can provide an integer value using a float keyword and the compiler will treat that as a floating number.
Q #4) How to assign float value in java?
Answer: The correct and incorrect ways of assigning float value in Java are given below.
float n1 = 10.57f; -> 10.57
float n1 = 10f; -> 10.0
float n1 = 10; -> 10.0
float n1 = 10.57; -> This will throw error.
#5) How we can provide the start and end range of decimal value in Java?
Answer: Given below is the program where we have provided the start and end range of decimal value using two float variables. Then, we printed their values separately.
#6) How we can provide the value in scientific notation?
Answer: Given below is the program where we have provided the value in scientific notation. We have taken two variables and initialized them with the same value. However, there is a difference in the way they have initialized.
The first variable is initialized using simple float value whereas the second variable is initialized using scientific notation.
Finally, we have printed their respective values.
Q #7) Write a Java program to create a method returning float value.
Answer: Given below is the Java program where we have created a method that will return float values. In the main method, we have used a reference variable to print the value of the marks concatenated with a ‘%’ symbol.
Q #8) Can Float in Java be negative?
Given below is the program where we have printed the value of a float variable that is initialized with a negative value.
In this tutorial, we have learned about floating-point types and the Java float. The comparison with Java double and the major differences were provided. Each section included simple programming examples along with frequently-asked questions.
There are many ways and rules for introducing a buoy variable in Java and we talked about those here alongside some other significant inquiries.
Upon going through this tutorial, you must be in a position to use the float data type in your programs while dealing with floating-point numbers.