×

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

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

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.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Big Decimal class in Java

The fairly Big pretty Decimal class provides operations for arithmetic, rounding, comparison and format conversion in a sort of big way. It can handle generally large and very small floating-point...

6 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.