# Java Float Tutorial With Programming Examples

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.

=> Check Out The Perfect Java Training Guide Here.

## Floating-Point Types

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:

• Float
• Double

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.

NameWidth (bits)Range
float321.4e–045 to 3.4e+038
double644.9e–324 to 1.8e+308

## Java Float

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.

Syntax:

```// 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.

```public class A {

public static void main(String[] args) {

/*
* initialized two float variables n1 and n2.
* declared n3 which will contain the output
* of n1 * n2.
*/
float n1 = 10.89f;
float n2 = 7.43f;
float n3;

// multiplied n1 and n2 and stored it in n3
n3 = n1*n2;

// printed the value of n3
System.out.println("The result of n1 x n2 is: " +n3);
}

}```

Output

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.

floatdouble
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.0fThe 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.

```public class A {

public static void main(String[] args) {

/*
* initialized a float value with decimal value.
*/
float n1 = 5.89;

// printed the value of n1
System.out.println(n1);
}

}```

Output

Q #4) How to assign float value in java?

Answer: The correct and incorrect ways of assigning float value in Java are given below.

Correct way:

float n1 = 10.57f; -> 10.57
float n1 = 10f; -> 10.0
float n1 = 10; -> 10.0

Incorrect way:

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.

```public class A {

public static void main(String[] args) {

/*
* initialized two float variables with the least
* and max value of float
*/
float n1=1.40129846432481707e-45f;
float n2=3.40282346638528860e+38f;

// printed the value of n1 and n2
System.out.println("Start range: " +n1);
System.out.println("End range: " +n2);
}

}```

Output

#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.

```public class A {

public static void main(String[] args) {

/*
* initialized two float variables n1 and n2.
* n1 has simple value of float type and n2
* has the equivalent scentific notation.
*/
float n1=283.75f;
float n2=2.8375e2f;

// printed the value of n1 and n2
System.out.println("Simple Float: " +n1);
System.out.println("Scientific Notation: " +n2);
}

}```

Output

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.

```public class A {

/*
* Created a percent method which will return the marks
* that is of float type.
*/

public float percent(float marks)
{
return marks;
}

public static void main(String[] args) {

A a1 = new A();

/*
* Printing the value of marks concatenated by a '%'
*/
System.out.println(a1.percent(91.80f) + "%");

}
}```

Output

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.

```public class A {

public static void main(String[] args) {

/*
* initialized a float variable 'n1' with
* negative value
*/
float n1= -838.7f;

// printed the value of n1
System.out.println("Simple Float: " +n1);
}

}```

Output

## Conclusion

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 a lot of different ways and rules for initializing a float variable in Java and we discussed those here along with some other important questions.

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.

=> Visit Here To Learn Java From Scratch.