Академический Документы
Профессиональный Документы
Культура Документы
php
Question 1 :
With generics the compiler has more information about the types of the objects, so explicit casts don't have to be used and the compiler can produce
type safe code.
What implications have the generics for the runtime performance of the program which uses them?
a)With the generics the compiler can optimize the code for used types. This and the omission of the casts are the reasons why the code compiled
with the generics is quicker than the one compiled without.
b)The usage of generics has no implications for the runtime performance of the compiled programs.
c)The improved flexibility and type safety means that the compiler has to generate concrete implementation from the generic template for each
used type. This means that applications start a bit slower.
Question 2 :
As an example for a generic class we will use a very simple container. A Basket can contain only one element.
public E getElement() {
return element;
}
}
class Fruit {
}
a)The source code is OK. Neither the compiler will complain, nor an exception during the runtime will be thrown.
Question 3 :
Let's stay with our baskets. What do you think about the following source code?
a)e source code is OK. Neither the compiler will complain, nor an exception during the runtime will be thrown
Question 4 :
Question 5 :
Let's have a look at the typeless baskets and the ones where the type is an unbounded wildcards.
// Source A
Basket<?> b5 = new Basket<Apple>();
b5.setElement(new Apple());
Apple apple = (Apple) b5.getElement();
// Source B
Basket b = new Basket();
b.setElement(new Apple());
Apple apple = (Apple) b.getElement();
// Source C
Basket b1 = new Basket<Orange>();
b1.setElement(new Apple());
Apple apple = (Apple) b1.getElement();
Source C will be compiled with warning(s). A ClassCastException exception will be thrown during the runtime
Question 6 :
d)The source code will be compiled with warning(s). During the runtime a ClassCastException will be thrown in the line 5.
e)The soure code will be compiled with warning(s). No exception will be thrown during the runtime.
Question 7 :
In our rich class hierarchy the class Apple has following subclasses:
Our fruit processing application contains an utility class which can decide, whether an apple is ripe:
class FruitHelper {
public static boolean isRipe(Apple apple) {
...
}
}
In the class FruitHelper we want to implement a method which can look into any basket which can contain apples only and decide, whether the
apple in the basket is ripe of not. Here the body of the method:
{
Apple apple = basket.getElement(); // 1
return isRipe(apple); // 2
}
Question 8 :
Now we want to implement a method which inserts only ripe apples into the basket. Here the method's body:
Question 9 :
We could acquire some expertise in the orangeology and now we can decide whether an orange is ripe or not - and this in pure Java. Now we want
to extend the class FruitHelper.
Question 10 :
Question 11 :
The accounting departement needs to know, how many baskets we produce. So we've changed the class Basket:
...
Basket() {
++theCount;
}
...
}
a)1
b)2
c)Compiler error
Question 12 :
Question 13 :
c)Compiler warning in the lines 3 and 4. An exception will be thrown in the line 7.
Question 14:
The last question is about arrays and generics. Which of the following lines can be compiled?
Answers:
Answer1 :
b)The usage of generics has no implications for the runtime performance of the compiled programs
The Java Virtual Machine and the copiled byte code are Generics agnostic. The comiled byte code does not differ from byte code compiled from
sources which don't use the generics.
So using the generics has no impact on the runtime performance of compiled Java code.
Answer2 : c
The line 2 is ok. The line 3 however will cause a runtime error. The result type of the methode getElement of Basket<Fruit> is Fruit. We cannot
assign a Fruit to a variable of the type Apple without a cast.
Answer3 : d
Both Apples and Oranges are Fruits and can be inserted into a Basket<Fruit>. That is why the cast is necessary in the line 3.
During the runtime the JVM checks the cast in line 3 and throws a ClassCastException since an Apple is not a Orange.
Answer5 : a,b
a) The compiler does not know the type of the element stored in b5. That is why it cannot guarantee an apple can be inserted into the basket
b5. So the statement s5.setElement(new Apple()) ist not allowed. The methode b5.setElement(..) cannot be used at all.
b) The compiler does not know the type of the element stored in b. That is why it cannot guarantee apples can be inserted into the basket b. But
since we did not specify the type of the element of b at all, the compiler will accept the source code and compile it as if it was a pre 1.5 source
code. Since the compiler cannot assure the type safety of the compiled code, it will issue a warning.
Answer6 : d
c) No, a cast is not necessary. In a type safe code bO can contain only an orange. But our code is not type safe, because we use the generic class
Basket<E> without specifying the concrete type for the type variable E. That is why we will be warned by the compiler.
Answer7 : d, e
Answer8 : d
a)The call insertRipe(new Apple(), new Basket<Fruit>()); would not be allowed. But it is possible to insert an Apple into a
Basket<Fruit>.
c)A bit better, but the call insertRipe(new Jonagold(), new Basket<Jonagold>()); would not be allowed. But it is possible to insert a
Jonagold into a Basket<Jonagold>.
Answer9 : b
No, unfortunately this source code cannot be compiled. Method names can only be overloaded if the count or the types of the methods differ.
The compiler erases the type information form the generics and so both isRipeInBasket methods have the same signature. This is not allowed.
(This is a major difference to the templates in C++)
Answer10: a
This source code is OK. The compiler converts the signatures of the insertRipe methods to the following ones:
Answer1 1: b
There is only one compiled class Basket and so is there only one static variable Basket.theCount.
Answer1 2: e
During the runtime the JVM has no information about the types used in the type variable E. That is why it cannot check to see, whether the cast
(Basket<Apple>)b is valid. The cast is reduced to (Basket)b and that is why there will be no ClassCastException the line 3. (But the
compiler will warn us about the type unsafety)
Answer1 3: b
Well, according to the Tutorial the choice b) shold be correct. JDK 1.5.0 Beta 1 behaves like described in c). Beta 2 corrects the behavior.
Answer1 4: c & d
Arrays of generic types are only allowed, if the values of the type variables is the unbounded wildcard.