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
- Integral 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