liveBook · Manning

Long literal

As explained in 100 Java Mistakes and How to Avoid Them

A Long literal is a numeric literal whose syntax specifies that its value has the Kotlin Long integer type.

A long literal uses an uppercase `L` suffix to make a numeric constant a `long` value, such as `1_000_000L`. The suffix matters because Java evaluates an expression from its operands, not from the type of the variable receiving its result. Assigning an `int` expression to `long` happens only after that expression has already been computed. section 4.2.2

Prevent overflow before assignment

This conversion can overflow before its result reaches `microseconds`: `long microseconds = seconds * 1_000_000;` Both operands are `int`, so multiplication is performed as `int`. For values above 2,147 seconds, the result can silently overflow. Make one operand long instead: `long microseconds = seconds * 1_000_000L;` An explicit `(long)` cast also works. When converting units, `TimeUnit.SECONDS.toMicros(seconds)` avoids the manual calculation. section 4.2.2

Make a long shift start with a long value

A left shift takes its result type from the left operand. In `mask |= 1 << k`, the `1` is an `int`, so shifts use an amount modulo 32. Calling `setBit(40)` can therefore set bit 8 rather than bit 40. Use `1L << k` when building a mask for a `long` field, and test high-bit and boundary cases. section 4.12

Keep hexadecimal masks from sign-extending

A hexadecimal literal without `L` may be an `int` even when assigned to `long`. `0x0000_0000_FFFF_FFFF` represents `-1` as an `int`; widening preserves that value, producing a long with every bit set. Thus `value & mask` leaves `value` unchanged. Write the intended mask as `0x0000_0000_FFFF_FFFFL`. Prefer uppercase `L`: lowercase `l` can resemble `1`. section 4.8

Use long literals deliberately when numeric operations must occur at long width:

  • Add `L` to a literal operand when multiplication is assigned or passed as `long`, so overflow is not completed at `int` width. section 4.2.2
  • Start long bit masks and left shifts with `1L`, then test bit positions from 32 through 63. section 4.12
  • Suffix long hexadecimal masks with uppercase `L` to avoid signed `int` widening. section 4.8
  • Prefer isolated, well-tested utilities or API conversions over repeated handwritten bitwise and unit-conversion arithmetic. section 4.12 section 4.2.2

IDE support can enforce or flag some cases, but it is not a substitute for checking the expression type. Eclipse can apply an uppercase-long-suffix cleanup setting. IntelliJ IDEA provides inspections for integer multiplication or shifts implicitly cast to `long`, and for negative `int` hexadecimal constants used in a long context; SpotBugs can report integer multiplication cast to long. section 1.2 section 4.2.2 section 4.8

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains expression typing, shift behavior, signed widening, overflow consequences, tests, API alternatives, and selected static-analysis checks.
Examples
Uses microsecond conversion, a long bit-setting method, and a hexadecimal mask that accidentally widens from `int` to `long`.
What it assumes
Assumes familiarity with Java numeric expressions and uses concrete failures to clarify operand types, widening, and shift behavior.
What you can do after
Diagnose/debug with it by identifying `int` operands in long computations and correcting literals, casts, tests, or API usage.
How it connects
Connects numeric literals with integer overflow, bitwise shifts, hexadecimal constants, implicit casts, unit conversion, and static analysis.

Also explained in

Compare how 2 books explain Long literal →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.