×

Java float vs double

Java :

Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems in the year 1996 as java 1.0.

It is a high level, robust, secured programming language. Java consists of Java Virtual Machine (JVM) which uses the execution engine and converts the class file into executable file (.class file to .exe file). The execution engine consists of Interpreter and Just In Time(JIT) compiler. It consists of mainly the following primitive data types :

  • Boolean data type
  • Character data type
  • Numeric data type
    • Integral data type
      • Short data type
      • Integer data type
      • Long data type
      • Byte data type
    • Floating point data type
      • Float data type
      • Double data type

Let us consider the float and double data types and let us see the difference between float data type and double data type. Float and double data type come under floating data types in primitive data types.

Float :

Float is one of the data types used in java. It is a data type that is used to store the numerical data which consists of floating point. It always retrieves the data in the form of decimals. In other words we can say that it is used to store the fractional numbers.

It has range from 3.4e - 038  to +3.4e + 038. The default value of float data type is 0.0f . The wrapper class used for float data type is “Float”. Float has a width of 32 bits or 4 bytes in storage to represent a real number. We can give input to the float data type variable in  both static and dynamic ways.

We use float data type when it need more precised values. Inorder to represent the float data type then we need to append the letter f at the end of the number else it considers the number as double data type.

Syntax :

The declaration syntax for the float data type is as shown below :  

float var = number;

Let us see few programs using the float data type

Example 1 :

Let us print the default value for the float data type :

import java.io.*;
class Demo
{
public static void main ( String args [ ] )
{
float a;
int b;
System.out.println ( “ The default value for float data type is : “ + a );
System.out.println ( “ the default value for integer data type is : “ + b );
} // main method
} // Demo class

Output :

The output is:

The default value for float data type is : 0.0f
The default value for integer data type is : 0

Example 2:

Now let us store the value to the float data type and let us print the value that is stored in the data type:

import java.io.*;
class Demo1
{
public static void main ( String args [ ] )
{
 float f = 12.9683f ;
 float a = 897.24986f;
System.out.println ( “ The float value of f is : “ + f  );
System.out.println ( “ The float value of a is : “ + a );
} // main method
} // Demo1 class

Output :

The output is :

The float value of f is : 12.9683
The float value of f is : 897.24986

Scanner class in float :

Scanner class reads the data or to give the input in a dynamic way. We can also give the float data as input using scanner class. We use different methods for different data types. The method used in scanner class to read the float data type is nextFloat( ). It is used to read the floating data in a dynamic way. Inorder to access scanner class we need to install util package.

Example 3 :

Now let us read the data to the float variable in a dynamic way and let us print the value of the variable :

import java.io.*;
import java.util.*;
class Demo2
{
public static void main ( String args [ ] )
{
float f ;
System.out.println ( “ The default value of f is : “ + f );
scanner sc = new Scanner ( System.in ) ;
f = sc.nextFloat ( ) ; 
System.out.println ( “ The value of f is : “ + f );
} // main method
} // Demo2 class

Input 1:

19.782f

Output 1:

The output is :

The default value of f is : 0.0f
The value of f is : 19.782

Input 2:

26.5429f

Output 2:

The output is :

The default value of f is : 0.0f
The value of f is : 26.5429

Double:

Double is also one of the data type that is used in java. It is a data type that is used to store the numerical data which consists of floating point. It always retrieves the data in the form of decimals. In other words we can say that it is used to store the fractional numbers.

It has range from 1.7e - 038  to  +1.7e + 038. We can also say that the range of double data type is unlimited. The default value of double data type is 0.0d . The wrapper class used for float data type is “Double”. Double has a width of 64 bits or 8 bytes in storage to represent a real number.

 We can give input to the double data type variable in  both static and dynamic ways. In some cases as compared to double float works more faster than double. We use double data type when it need more precised values. If we won’t give any kind of letter representation at the end of the number then we can consider it as the double data type.

Syntax:

The declaration syntax for the float data type is as shown below :  

double var = number;

Let us see few programs using the float data type

Example 4:

Let us print the default value for the double data type :

import java.io.*;
class Demo3
{
public static void main ( String args [ ] )
{
double a;
int b;
float f;
System.out.println ( “ The default value for double data type is : “ + a );
System.out.println ( “ the default value for integer data type is : “ + b );
System.out.println ( “ The default value for float data type is : “ + a );
} // main method
} // Demo3 class

Output :

The output is:

The default value for double data type is : 0.0d
The default value for integer data type is : 0
The default value for float data type is : 0.0f

Example 5:

Now let us store the value to the double data type into a variable and let us print the value that is stored in the data type :

import java.io.*;
class Demo4
{
public static void main ( String args [ ] )
{
 double d =3498.7896;
 double d1 = 94.1254;
System.out.println ( “ The double value of d is : “ + d  );
System.out.println ( “ The double value of d1 is : “ + d1 );
} // main method
} // Demo4 class

Output :

The output is :

The double value of d is : 3498.7896
The double  value of d1 is :  94.1254

Scanner class in double:

Scanner class reads the data or to give the input in a dynamic way. We can also give the double data as input using scanner class. We use different methods for different data types. As we have read the float data type using the method nextFloat( ) we also use another method to read the number in double data type.

The method used in scanner class to read the double data type is nextDouble( ). It is used to read the double data in a dynamic way. Inorder to access scanner class we need to install util package.

Example 6 :

Now let us read the data to the double variable in a dynamic way and let us print the value of the variable :

import java.io.*;
import java.util.*;
class Demo5
{
public static void main ( String args [ ] )
{
double d;
System.out.println ( “ The default value of d is : “ + d );
scanner sc = new Scanner ( System.in ) ;
System.out.println(“Enter the number : “ );
d = sc.nextDouble ( ) ; 
System.out.println ( “ The value of d is : “ + d );
} // main method
} // Demo5 class

Input 1:

Enter the number : 36.8439

Output 1:

The output is :

The default value of d is : 0.0d
The value of d is : 36.8439

Input 2:

Enter the number : 2894.9834

Output 2:

The output is :

The default value of d is : 0.0d
The value of d is : 2894.9834

Now let us see the example with the difference between float data type and the double data type.

Example 7 :

Now in this example let us find the value of float and double variable by reading the value ion a dynamic way :

import java.io.*;
import java.util.*;
class Demo6
{
public static void main ( String args[ ] )
{
double d;
float f;
System.out.println(“The default value of double variable d is : “ + d );
System.out.println(“The default value of float variable f is : “ + f );
Scanner sc = new Scanner(System.in);
d = nextDouble( );
System.out.println( “ Enter the value of d : “ );
f = nextFloat( );
System.out.println( “ Enter the value of f : “ );
System.out.println(“The value of d is “ + d );
System.out.println(“The value of f is “ + f );
} // main method
} // Demo6 class

Input 1 :

Enter the value of d : 13.9834
Enter the value of f : 38.9634f

Output 1 :

The default value of double variable d is : 0.0d
The default value of float variable f is : 0.0f
The value of d is : 13.9834
The value of f is : 38.9634

Input 2 :

Enter the value of d : 9.4872
Enter the value of f : 69.3829f

Output 2 :

The default value of double variable d is : 0.0d
The default value of float variable f is : 0.0f
The value of d is : 9.4872
The value of f is : 69.3829

Related Topics

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.