Scientific Notation in Floating-Point Literals
What you'll learn: How to write very large or very small floating-point numbers using exponential notation in Java.
When working with floating-point numbers, you'll sometimes encounter values that are extremely large (like the speed of light in meters per second: 299,792,458) or incredibly small (like the mass of an electron: 0.00000000000000000000000000000091093837 kilograms). Writing all those zeros is tedious and error-prone.
What is Scientific Notation?
Scientific notation lets you express numbers as a base value multiplied by 10 raised to a power. In Java, you write this using the letter e or E (for "exponent"). The format is:
number e power
For example:
1.5e10means 1.5 × 10¹⁰ (or 15,000,000,000)3.8e-5means 3.8 × 10⁻⁵ (or 0.000038)
Using Scientific Notation in Code
You can use scientific notation when declaring and initializing float and double variables:
double speedOfLight = 2.998e8; // 299,800,000 meters/second
double electronMass = 9.109e-31; // 0.000...000091093 kg
float smallValue = 1.23e-4f; // Remember the 'f' suffix for float!
Think of the e as saying "times ten to the power of..." The number after e is the exponent. Positive exponents make numbers larger (move the decimal right), while negative exponents make them smaller (move the decimal left).
Key Takeaway
Use scientific notation (e or E) in floating-point literals to write very large or very small numbers concisely, where the number after e represents the power of 10.