Converting and Casting Primitives

Every Java expression—whether arithmetic, a literal, or a method call—has a type. When the result of an expression has to be assigned to a variable or used in another expression, a different type may be required. The Java compiler is allowed to perform some type conversions automatically but rejects others unless the programmer provides a specific cast. This section discusses the rules the compiler uses.

Widening Conversions

Generally speaking, the Java compiler is allowed to perform primitive conversions that do not lose information about the magnitude of the value. It is also allowed to perform some conversions of integers to floating-point values that lose some precision due to the impossibility of representing ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.