- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java float Keyword
Usage and Examples
float
Keyword in Java
The float
keyword in Java is used to declare variables that hold single-precision 32-bit IEEE 754 floating point numbers. It's a primitive data type mainly used when memory savings are more critical than absolute precision.
Why Use float
Instead of double
?
While double
is the default for floating point numbers due to its higher precision, float
can be beneficial in scenarios like:
- Memory-constrained applications (e.g., mobile or embedded systems)
- Large arrays of decimal values where precision can be compromised
Syntax
float variableName = value;
Important Notes
- Values assigned to a
float
must end withf
orF
to indicate it’s a float literal. - Without the suffix, a decimal value is treated as a
double
by default.
Example: Declaring a Float Variable
public class FloatExample {
public static void main(String[] args) {
float price = 19.99f;
System.out.println("The price is: " + price);
}
}
The price is: 19.99
Assigning Double to Float (with Casting)
Since double
has higher precision, assigning it to a float
without explicit casting will cause a compile-time error.
public class FloatCasting {
public static void main(String[] args) {
double d = 45.6789;
float f = (float) d;
System.out.println("Float value after casting: " + f);
}
}
Float value after casting: 45.6789
Default Value of float
When declared as an instance variable without initialization, the default value of a float is 0.0f
.
public class DefaultFloatValue {
static float temperature;
public static void main(String[] args) {
System.out.println("Default float value: " + temperature);
}
}
Default float value: 0.0
Using float
in Arithmetic Operations
Float values behave like you'd expect in arithmetic expressions. But remember, due to precision limits, some results may not be exact.
public class FloatMath {
public static void main(String[] args) {
float a = 10.5f;
float b = 4.2f;
float sum = a + b;
System.out.println("Sum: " + sum);
}
}
Sum: 14.7
Precision Limitations
Float values are less precise than double, often accurate up to 6-7 decimal digits. Let's look at a precision test:
public class FloatPrecision {
public static void main(String[] args) {
float f1 = 1.123456789f;
System.out.println("Float value: " + f1);
}
}
Float value: 1.1234568
Common Mistake: Missing 'f' Suffix
New learners often forget the 'f' suffix and get a compile-time error.
public class FloatError {
public static void main(String[] args) {
float value = 3.14; // Compile-time error
}
}
Fix: Use float value = 3.14f;