Types 1

What Are Types?

Types in Java/Scala

Strong Typing

Static and dynamic typing

Type Conversions

Subtypes

Subtypes in Java

Transitive Relations

Transitive Closure

Example: If R is >1 and T is :>, then Object :> FileInputStream because there is a chain

Lab

???

Step 1: Type Conversions

  1. Consider this simple Java program.
    public class Test
    {
       public static void main(String[] args)
       {
          int x = 1729;
          double y = x;
       }
    }

    Compile and run javap -c Test. Which virtual machine instruction is implicitly generated to carry out the type conversion?

  2. Now change the body of main to
          double x = 17.29;
          int y = (int) x;

    Compile and run javap -c again. Now which virtual machine instruction is generated for the conversion?

  3. Why did the compiler require a cast only in the second example even though a type conversion was carried out in both cases?

Step 2: Autoboxing

  1. Change the body of main to
          int x = 1729;
          Integer y = x;

    What code is generated for the type conversion?

  2. Now change the body of main to
          Integer x = 1729;
          Integer y = x + 1;

    What code is generated for auto-unboxing?

  3. Integer objects are immutable, but you can call x++ on an Integer x. Use javap -c to find out what actually happens. What was your sample program, what is the relevant part of the disassembly output, and why does that not contradict the immutability of Integer objects?

Step 3: Subtypes

Consider variables

Object o = null;
Object[] oa = null;
InputStream i = null;
InputStream[] ia = null;
FileInputStream f = null;
FileInputStream[] fa = null;
  1. Write down what you think are the direct supertype relationships between the six types Object, Object[], ... FileInputStream[], using the >:1 notation
  2. Write down what you think are the indirect supertype relationships (that is, all >: that are not >:1) between the six types Object, Object[], ... FileInputStream[]
  3. Place the variable declarations and 30 assignment statements o = oa; o = i; o = ia; o = f; o = fa; oa = o; oa = i; oa = ia; oa = f; oa = fa; i = o; ... into a main method and compile. You should get an error message for each line that does not correspond to a supertype relationship. Did you find an error in your list in step 2? (Most people do.) If so, which?
  4. Consider a main program with this body:
    FileInputStream f = new FileInputStream("Test.java");
    InputStream i = f;

    Run javap -c and peek at the byte codes. What type conversion instruction did the compiler generate?

Step 4. Array Variance

Consider this program.

import java.io.*;
import java.net.*;

public class Test
{
   public static void main(String[] args) throws MalformedURLException,
      IOException
   {
      FileInputStream[] fa = new FileInputStream[2];
      InputStream[] ia = fa;
      ia[0] = new URL("http://horstmann.com").openStream();
   }
}
  1. Does it compile? If so, do you get any warnings? If not, do you get any compiler errors?
  2. What happens when you run the program?
  3. Explain the reason for the exception.