Java Float Keyword
Float:
- In general, there are two categories of data types: primitive data types and non-primitive data types.
- So, float is the data type which is a primitive data type.
- Declarating the variables and procedures is done with it. It displays fractional numbers.
- A range of 1.40129846432481707e-45 to 3.40282346638528860e+38 is covered by the float (positive or negative).
- The default setting of the float data type is 0.0f.
- The default size of the java float keyword is 4 bytes.
- Using float for precise numbers like currency is not a good strategy.
Therefore, the meaning of the keyword "float" is a number, a single-precision integer that requires 32 bits or 4 bytes of memory. Working with such numbers is quicker on some processors, and as was already said, they are smaller than double-precision numbers. However, making a concise and unambiguous statement is impossible. Let's assume that some contemporary CPUs process double precision numbers more quickly.
Java's float and double types serve as representations for float values (also known as floating point numbers or real numbers). To save data up to a specific sign after the decimal point, these keywords are used.
Java's float and double types serve as representations for float values (also known as floating point numbers or real numbers). To save data up to a specific sign after the decimal point, these keywords are used.
Syntax of java float keyword:
floatvariable_name = assign value
Example:
float N = 5.75f;
System.out.println(N);
Examples of Java float keyword:
Example 1
It is a simple example to display or show the float type variable in the program using the java programming language.
public class Float_Ex_1
{
public static void main (String [] s) {
float S1=10.5f;
float S2=10f;
System.out.println("S1: "+S1);
System.out.println("S2: "+S2);
}
}
Output:
S1: 10.5
S2: 10
Example 2:
From this below given program we provide or assign the integer value to float variable. In this case, the compiler implicitly types offloatto integer, displaying the resulting value in fractional form.
public class Float_Ex2 {
public static void main (String [] s) {
float S1=10;
float S2=20;
System.out.println("S1: "+S1);
System.out.println("S2: "+S2);
}
}
output
S1: 15.0
S2: 20.0
Example 3:
Here this program is provided with a large or bigger decimal value to a variable.
public class Float_Ex3 {
public static void main (String [] as) {
float D1=581216732.323433f;
float D2=7.83684987683688f;
System.out.println("D1: "+D1);
System.out.println("D2: "+D2);
}
}
Output:
D1: 5.812167E8
D2: 7.8368497
Example 4
We provide or assign the float value to the decimal variable for testing of the floating-pointdata type float.
public class Float¬_Ex4 {
public static void main (String [] s) {
float N=56.34;
System.out.println("N1: "+N);
}
}
Output:
java. lang exception in thread "main" Error: unfixed compilation issue
Example 5
Here let us assign the end range to the decimal value which is provided to the variable declared in the program.
public class FloatEx5 {
public static void main (String [] s) {
float P1=1.40129846432481707e-45f;
float P2=3.40282346638528860e+38f;
System.out.println("P1: "+P1);
System.out.println("P2: "+P2); }
}
Output:
P1: 1.4E-45
P2: 3.4028235E38
Example 6:
Providing the value in scientific notation
public class FloatEx6 {
public static void main (String [] s) {
float N1=1873.2f;
//giving the same value in scientific notation
float N2=1.8732e3f;
System.out.println("N1: "+N1);
System.out.println("N2: "+N2);
}
}
Output:
N1: 1873.2
N2: 1873.2
Example 7
Here we create a method returning float value to the main method.
public class FloatEx7 {
public float display (float wht)
{
return wht;
}
public static void main (String [] s) {
FloatEx7 p=new FloatEx7();
System.out.println(p. display (62.5f));
}
}
Output:
62.5
Example 8:
Addition of two flaoting numbers or variables
FloatExample {
public static void main (String [] s) {
//variables declaration
float Number1=2182818284590.45f;
float Number2=19822612787260.141592181f;
System.out.println("Number1 = " +Number1);
System.out.println("Number2 = " + Number2);
System.out.println("Number1 + Number2 = " + Number1 + Number2);
}
}
Output:
Number1 = 2.1828183E12
Number2 = 1.98226121E13
Number1 + Number2 = 2.1828183E121.98226121E13
Example 8:
Program to print special floating-point numbers:
public class Float¬_Ex {
public static void main (String [] s) {
int my = 1;
float hero = 0.0f;
double nhero = -0.0;
double N = Double.NEGATIVE_INFINITY;
double P = Float.POSITIVE_INFINITY;
System.out.println(my / hero);
System.out.println(my / negZero);
System.out.println(zero == nhero);
System.out.println(N* 0);
System.out.println(P+N);
}
}
Output:
Infinity
-Infinity
true