杰瑞科技汇

Java中double与long该如何选择使用?

Of course! This is a fundamental concept in Java that often confuses beginners. Let's break down double and long in detail, comparing them across several key aspects.

Java中double与long该如何选择使用?-图1
(图片来源网络,侵删)

The Short Answer

Feature long double
Purpose For whole numbers (integers). For numbers with decimal points (floating-point).
Type Integer Floating-Point
Size 64 bits (8 bytes) 64 bits (8 bytes)
Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ~ ±1.8 x 10³⁰⁸ (with ~15-17 significant decimal digits)
Example 15000000000L 14159 or 5e10
Suffix L (or l) d (or D) - usually optional

Detailed Breakdown

long - The Whole Number Champion

The long data type is used to store integer values. Integers are whole numbers without any fractional or decimal component.

Key Characteristics:

  • Type: It's an integral type, meaning it represents a subset of real numbers (the integers).
  • Size: It occupies 64 bits of memory. This gives it a very large range.
  • Range: It can store values from -2⁶³ to 2⁶³ - 1, which is approximately -9 quintillion to +9 quintillion.
  • Precision: It has perfect precision for any integer within its range. The number you store is the exact number you get back. 1000000000000000000L is stored exactly.

When to use long:

Use long when you need to store a whole number that is too large to fit into an int. The default type for an integer literal (like 100) is int. If the number is larger than what an int can hold, you must add an L or l suffix to tell the compiler it's a long.

Example:

// An int can hold up to about 2 billion.
int population = 8_000_000_000; // COMPILE ERROR! This number is too big for an int.
// This is correct. The 'L' suffix makes it a long literal.
long worldPopulation = 8_000_000_000L;
long distanceToSunInKm = 149_597_870L;
// You can perform arithmetic
long anotherNumber = worldPopulation + 100;
System.out.println(anotherNumber); // 8000000100

double - The Decimal Number Specialist

The double data type is used to store floating-point numbers, which are numbers that can have a fractional part (decimals).

Java中double与long该如何选择使用?-图2
(图片来源网络,侵删)

Key Characteristics:

  • Type: It's a floating-point type. It's designed to approximate a wide range of real numbers, including those with decimals.
  • Size: It also occupies 64 bits of memory, just like long.
  • Range: It has an enormous range, from about -1.8 x 10³⁰⁸ to 1.8 x 10³⁰⁸. This makes it suitable for scientific calculations.
  • Precision: This is the most important concept to understand about double. It has limited precision. It can only store about 15-17 significant decimal digits accurately. It does not store decimals exactly.

Why does double have limited precision? It stores numbers in a scientific notation format (sign, exponent, mantissa) in binary. Just like 1/3 is 333... and cannot be written exactly as a finite decimal, many fractions (like 0.1) cannot be written as a finite binary number. This leads to small rounding errors.

Example:

double price = 9.99;
double pi = 3.141592653589793;
// This classic example shows the precision issue
double tricky = 0.1 + 0.2;
System.out.println(tricky); // Prints: 0.30000000000000004
// The default type for a decimal literal is double.
double anotherDouble = 3.14; // No 'd' needed, but it's good practice.
double scientificNotation = 1.5e10; // This is 15,000,000,000.0

Comparison Table: long vs. double

Aspect long double
Data Type Integral (Integer) Floating-Point
What it Stores Whole numbers only (e.g., -10, 0, 100, 999999) Numbers with decimal points (e.g., -10.5, 0.0, 99.99)
Memory Size 64 bits (8 bytes) 64 bits (8 bytes)
Value Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ~ ±1.8 x 10³⁰⁸
Precision Perfect for integers within its range. Approximate. About 15-17 significant decimal digits.
Default Literal Suffix L (e.g., 10000000000L) d (e.g., 9d), but often optional (9)
Use Case Counting items, IDs, money (in cents), large integers. Scientific calculations, financial calculations (with caution), physics, graphics.

Critical Differences and Common Pitfalls

Precision is King

This is the most important difference.

  • long: long bigNumber = 1234567890123456789L; is stored exactly.
  • double: double bigDouble = 1234567890123456789.0; might lose precision because it can't hold all those significant digits accurately.

You Cannot Assign a double to a long (Implicitly)

Because a double can have a fractional part and might lose precision, Java will not let you assign it directly to a long. You must explicitly cast it, which tells the compiler you are aware you might lose information (the decimal part).

double myDouble = 99.99;
// long myLong = myDouble; // COMPILE ERROR! Incompatible types.
// You must cast it
long myLong = (long) myDouble; // This truncates the decimal part. myLong becomes 99.
double anotherDouble = 1.2e20;
long anotherLong = (long) anotherDouble; // This works, but you lose precision.
System.out.println(anotherLong); // Prints 120000000000000000000

Arithmetic Operations behave differently

  • long arithmetic is exact.
  • double arithmetic can accumulate small errors.
long l1 = 1000000000000000000L;
long l2 = 1L;
System.out.println(l1 + l2); // 1000000000000000001 (Correct)
double d1 = 1000000000000000000.0;
double d2 = 1.0;
System.out.println(d1 + d2); // 1.0E18 (Incorrect! The '1' is lost due to limited precision)

Which One Should I Use? A Decision Guide

  • Use long if:

    • You are counting something (e.g., number of users, items in a cart).
    • You are working with IDs that are very large.
    • You need to store a whole number that is outside the range of an int.
    • Perfect precision is absolutely required.
  • Use double if:

    • You need to perform calculations with decimal numbers (e.g., scientific, engineering, or statistical data).
    • You need to represent a very wide range of numbers, both large and small.
    • You are working with percentages or non-integer measurements.
  • A Special Note on Money: For financial calculations, neither long nor double is ideal. double's precision errors can lead to incorrect monetary totals. The standard best practice is to use long to store the value in the smallest unit (e.g., cents instead of dollars) to avoid decimal inaccuracies.

    // Good way to handle money
    long priceInCents = 999; // Represents $9.99
    long quantity = 2;
    long totalInCents = priceInCents * quantity; // 1998 cents ($19.98)
分享:
扫描分享到社交APP
上一篇
下一篇