float data type in Java
It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of accuracy.
nanadwumor
- The float data type is a floating point number.
- It has single precision data type
- The float type is stored in 32 bits of memory just like int.
RECOMMENDED ARTICLES
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...
double data type in Java
The double data type is also a floating point number. It is called double because it has a double-precision decimal number. The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters onto the console The print...
A floating point number is a number with a fractional part or decimal component. These types of numbers are needed in certain situations. Example, if you write a program that calculates precise quantities, you will need decimal points in your number. This is called floating point number in programming.
In Java, the float and double data types are used for floating point numbers.
Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!
It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of accuracy.
This implies that if we get past six decimal points, the number becomes less precise and more of an estimate.
The float type is stored in 32 bits of memory just like int. 8 bits make a byte. Thus, float has 4 bytes of memory.
Despite having same bits of memory as int, float has a different range from the int. This is due to its floating decimal point.
This type can represent positive and negative numbers. It has a range of 1.40239846 x 10-45 and 3.40282347 x 1038.
The default value of a float is 0.0 but not 0 as in int.
Declaring an float variable
We declare floats the same as any other type :
float age;
float height;
Alternatively, you can also declare all float variables in a row. That’s,
float age, height, weight;
Program demonstrates float data type
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Program demonstrates the float data type public class FloatVariable { public static void main(String[] args) { float weight=40.5f; //lowercase f makes the 40.5 a float value float height = 5.5F; //uppercase F makes the 5.5 a float value System.out.println("My weight is " + weight + " pounds and my height is " + height + " feet."); } } |
Output
Note that we added f or F to the end of the float literals. In Java, every floating number is by default treated as a double. Adding f or F to the value tells the compiler that it is a float but not a double. If we fail to add f or F, Java will throw an error. Example:
float height=40.5; //error
Height is a float variable but 40.5 is resolved into a double by the compiler by default. This creates an error.
You cannot store a double (which has a bigger memory range) into a float.
You should attach either f or F to the value to explicitly make the number a float.
float height=40.5f; //correct
You May Also Like…
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect...
double data type in Java
The double data type is also a floating point number. It is called double because it has a...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the...
0 Comments