int in Java. Set of numbers between
Integer.MIN_VALUE and Integer.MAX_VALUE.
Operations +, -, *, etc.int short long byte char float double
booleannull. (Null type)(String, Int)(String, String) => Int"Hello".println().
String has no such methodString[] words = { "Hello", "World" };
Object[] objs = words; // compiles: Can convert String[] to Object[]
objs[0] = new Watermelon(); // compiles. Can convert Watermelon to Object
// Oops: Now words[0] is a Watermelon!
Causes ArrayStoreException at run-time.
int luckyNumbers[] = { 17, 29 };
double* values = (double*) luckyNumbers; // compiles
double x = values[0]; // may not be a legal floating-point value
NullPointerException, ArrayStoreExceptionval x = "Hello" ... x = new Duck(); ... x.quack()
Variables have no types, but values do. Errors caught at run-time ("Duck typing").
val x = "Hello" // x is inferred to be String
int → double changes from 2's
complement to IEEE floating pointint → Integerint n = 17; double x = n; Double d = x;
n = (int) x;
operator
functionsimplicit functions
implicit def makeAction(action : (ActionEvent)=>Unit) = new ActionListener {
def actionPerformed(event: ActionEvent) { action(event) }
}
InputStream has method read with
defined behavior (returns next byte, -1 at end of stream)FileInputStream has method read with same
behaviorpublic void read(InputStream in) // Ok to call with FileInputStream object
- The subtype and supertype relations are binary relations on types. The supertypes of a type are obtained by reflexive and transitive closure over the direct supertype relation, written S >1 T, which is defined by rules given later in this section. We write S :> T to indicate that the supertype relation holds between S and T.
s is a variable of type S, t is a
variable of type T, then the assignment s = t is okT extends S or T implements S, then S
>1 T[] :>1
T[]Object :>1 S[] boolean. true (infix notation)x R y and y R z ⇒ x R z
Object >1 InputStreamInputStream >1
FileInputStreamObject is not a direct supertype of
FileInputStreamx T y is true if there are elements x1, x2, ... xn such that
x R x1, x1 R x2, ..., xn - 1 R xn, xn R y
Example: If R is >1 and T is :>, then Object
:> FileInputStream because there is a chain
Object >1 InputStream
>1 FileInputStream

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?
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?
main to
int x = 1729;
Integer y = x;
What code is generated for the type conversion?
main to
Integer x = 1729;
Integer y = x + 1;
What code is generated for auto-unboxing?
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?Consider variables
Object o = null; Object[] oa = null; InputStream i = null; InputStream[] ia = null; FileInputStream f = null; FileInputStream[] fa = null;
Object, Object[], ...
FileInputStream[], using the >:1 notationObject, Object[], ...
FileInputStream[]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? 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?
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();
}
}