When we require accuracy then we can use Floating point Data types. It is also called as Real number.
Floating Data Type have respective Wrapper Class Float or Double.
In Java, there are two floating point Data types, let see one by one
1. Float
Float Data type represent single and double precision numbers. It uses 4 bytes for storage space.
It is very useful when we require accuracy with small degree of precision. Default value is 0.0f.
Example of float Data type
package BestJavaTutorial;
class Simple
{
public static void main(String args[])
{
float a=10.0f;
int b=5;
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
float ans;
ans=a+b;
System.out.println("Answer is: "+ans);
}
}
run:
Value of a: 10.0
Value of b: 5
Answer is: 15.0
BUILD SUCCESSFUL (total time: 0 seconds)
2. Double
Double Data type represent double precision numbers. It uses 8 bytes for storage space.
It is useful for large degree of precision. It is generally used as default data type for decimal values, generally the default choice.
Double data type should never be used for precise values such as currency. Default value is 0.0d.
Example of double Data type
package BestJavaTutorial;
class Simple
{
public static void main(String args[])
{
double a=10000.005d;
int b=5;
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
double ans;
ans=a+b;
System.out.println("Answer is: "+ans);
}
}
run:
Value of a: 10000.005
Value of b: 5
Answer is: 10005.005
BUILD SUCCESSFUL (total time: 0 seconds)
Tags:
Core Java