JMU
Type Conversion
Implicit and Explicit


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


A Common Approach to Type Matching
Type Matching Reconsidered
Unsigned Integers
Unsigned Integers (cont.)
Going Further: Twos Complement
Going Further: Twos Complement (cont.)
Floating Point Numbers
Floating Point Numbers (cont.)
Implicit Widenings in Java
int   small;
long  large;

.
.
.
large = small;
Implicit Numeric Promotion in Java
Explicit Narrowings in Java
Narrowing Conversions (cont.)
Narrowing Conversions (cont.)
Narrowing Conversions (cont.)
public class NarrowingExample
{
  public static void main(String[] args)
  {
    float fmax, fmin;

    fmax = Float.POSITIVE_INFINITY;
    fmin = Float.NEGATIVE_INFINITY;

    JMUConsole.println("long:  "+(long)fmin +".."+(long)fmax);
    JMUConsole.println("int:   "+(int)fmin  +".."+(int)fmax);
    JMUConsole.println("short: "+(short)fmin+".."+(short)fmax);
    JMUConsole.println("byte:  "+(byte)fmin +".."+(byte)fmax);
  }
}