×

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

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.