Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Conversions

Implicit conversions (JPL pg 121)

  • conversions which happen automatically
  • any primitive type value can be converted to a type which supports a larger value (widening primitive conversion)
  • implicit conversion occurs from integer to floating point values but not vice versa
  • you can use an object of one type wherever a reference to one of it's supertypes is required ie you can reference up the class hierarchy but not down
  • you can assign a null object reference to any object reference

Explicit conversion (JPL pg 122)

  • when one type cannot be assigned to another type through implicit conversion you can use the cast operator

Identity Conversion (JLS §5.1.1)

  • any type can be converted to it's own type
  • only conversion allowed for boolean primitive type

Widening Primitive Conversion (JLS §5.1.2)

byte -> short -> int -> long -> float -> double
char -> int -> long -> float -> double
  • widening conversions of integer types preserve the exact original value of the number
  • runtime errors never occur as a result of widening conversion
  • which is why widening conversion does not allow byte and short values to be converted to char as the char type is unsigned while byte and short are signed; the byte and short would lose information
        byte  b =  126;
        short s = 1000;
        char  c;
        
        c = b;  // compile error: possible loss of precision
        c = s;  // compile error: possible loss of precision    
    
  • widening conversion of an int or long to a float may result in loss of precision however the new float value will be the correctly rounded equivalent of the original number
  • the same applies when a long is widened to a double

Narrowing Primitive Converson (JLS §5.1.3)

double -> float -> long -> int > char -> short > byte
  • narrowing primitive conversion may lose information about the overall magnitude of the number and may also lose precision
  • runtime errors never occur as a result of narrowing conversion because compile time errors occur if you try it; need to use cast operator
  • narrowing conversion loses all but the lowest bits (see Working with Binary, Octal and Hex numbers)
  • narrowing from floating-point numbers to integer numbers occurs within the following minimum and maximum values (values are rounded-toward-zero)
long:  -9223372036854775808..9223372036854775807
int:   -2147483648..2147483647
short: 0..-1
char:  0..65535
byte:  0..-1
  • if the floating-point value is NaN the result is an int or long value of zero

Widening Reference Conversion (JLS §5.1.4)

  • convert from any class, interface or array reference to an Object reference
  • convert from any class to any interface that it implements
  • convert from any class, interface or array type to a null reference
  • convert from any subinterface to any interface it extends
  • from any array to type Cloneable or type java.io.Serializable
  • from any array of references to an array of compatible reference types
  • the above conversions never produce a runtime error or require special action

You can't instantiate an interface reference as interfaces are always abstract

    SuperInterface si = new SuperInterface();   // compile-error

Narrowing Reference Conversion (JLS §5.1.5)

  • from Object to any other class, interface or array type
  • from any superclass to a subclass
  • from any non-final class to any interface as long as the class does not implement the interface
  • from any interface to any non-final class
  • from any interface to any final class providing the final class implements the interface
  • from any interface to any other non-superinterface and providing neither interface contains methods with the same signature
  • from any array of reference types to any other array of reference types as long as the types of each array are compatible under the Narrowing Reference rules

The above will be allowed at compile time but may throw a runtime ClassCastException if the types are not compatible

Summary
  • widening conversions do not require casts and will not produce compile or runtime errors
  • narrowing conversions require explicit casts. Will compile ok but may result in runtime ClassCastException errors

String Conversions

  • every other type, including null, can be converted to String

Method Conversion

  • each argument is converted to the type of the method parameters
  • widening conversion is implicit
  • narrowing conversion is not implicit (values must be cast)

Forbidden Conversions (JLS §5.1.7)

  • reference to primitive
  • primitive to reference (excepting String)
  • null to primitive
  • reference or primitive to boolean
  • boolean to reference (excepting String) or primitive
  • one class to another unless they have a superclass/subclass relationship (excepting String)
  • final class to interface unless the final class implements the interface
  • class to array unless the class is Object
  • array to any class other than Object or String
  • array to any interface other than java.io.Serializable or Cloneable
  • interface to interface if they contain methods with the same signature

Also see

Sun Tech Tip: Narrowing and Widening Conversions

Example Code

Traps

  • variables requiring narrowing conversion being passed to methods without using a cast
  • assigning a typed byte or short variable to a char variable


Conversions Promotion Overflow Unary Prefix Arithmetic
  Bin/Hex/Octal Bitwise Shift Comparison Logical Assignment
  Cast Ternary String equals() Precedence Bit vs Logic
  Method Invocation