Generics Flashcards Preview

JAVA OCA7 > Generics > Flashcards

Flashcards in Generics Deck (16)
Loading flashcards...
1

What property do generics add to collections?

They make them type-safe. We can not add any element other than specified type.

2

What do we call the type in brackets as above:

List myList = new ArrayList();

The type in angle brackets is referred to as the "parameterized type," "type parameter," or, of course, just old-fashioned "type."

3

List myList = new ArrayList();
for (String s : myList) {
int x = s.length();
}
do we need a cast in this statement?

No need for a cast before calling a String method! The compiler already knew "s" was a String coming from myList

4

Would that compile:
void takeListOfStrings(List strings) {
strings.add(new Integer(42));
}

No, strings is type-safe.

5

Can return types be type-safe?

Yes, like below:
public List getDogList() {
List dogs = new ArrayList();
// more code to insert dogs
return dogs;
}

6

Would that code work:

public class TestBadLegacy {
public static void main(String[] args) {
List myList = new ArrayList();
myList.add(4);
myList.add(6);
Inserter in = new Inserter();
in.insert(myList); // pass List to legacy code
}
}
class Inserter {
// method with a non-generic List argument
void insert(List list) {
list.add(new String("42"));
}
}

Yes it both compiles and runs. But compiler gives a warning.

7

How does generics work in compile vs run time?

Think of generics as strictly a compile-time protection. The compiler uses generic type information (the in the angle brackets) to make sure that your code doesn't put the wrong things into a collection and that you do not assign what you get from a collection to the wrong reference type. But NONE of this protection exists at runtime. Through a process called "type erasure," the compiler does all of its verifications on your generic code and then strips the type information out of the class bytecode.

8

Do you have type protection in Arrays for compile or runtime?

Yes. it gives you both compile and run-time protection. But for collections it does not work that way just to support the legacy code.

9

Do we need casting below?
List test = new ArrayList();
test.add(43);
int x = test.get(0);

Yes we have to add;
int x = (Integer)test.get(0);
Because unboxing can't convert a plain old Object to a primitive.

10

Do we need casting below?
List test2 = new ArrayList();
test2.add(343);
int x2 = test2.get(0);

No. Unboxing can unbox Integer to int since get returns Integer with generics here.

11

Is this declaration valid?
class Parent { }
class Child extends Parent { }
List myList = new ArrayList();

No, it doesn't work. There's a very simple rule here the type of the variable declaration must match the type you pass to the actual object type. If you declare List foo, then whatever you assign to the foo reference MUST be of the generic type . Not a subtype of . Not a supertype of . Just .

12

Is this declaration valid?
class Parent { }
class Child extends Parent { }
Parent[] myArray = new Child[3];

Yes

13

Can we assign the individual ArrayList of Animal subtypes (, , ) to an ArrayList of the supertype , which is the declared type of the argument.

No.
Ex;
AnimalDoctorGeneric.java:52: checkAnimals(java.util.ArrayList) in AnimalDoctorGeneric cannot be applied to (java.util.List) doc.checkAnimals(cats);

14

Is below code piece ok?

List animals = new ArrayList();
animals.add(new Cat());
animals.add(new Dog());

Yes

15

Is this declaration allowed? If yes what is the problem with that?

public void foo() {
Dog[] dogs = {new Dog(), new Dog()};
addAnimal(dogs);
}
public void addAnimal(Animal[] animals) {
animals[0] = new Dog();
}

The problem is when we change the caller, the problem is not realized by the compiler.

public void foo() {
Cat[] cats = {new Cat(), new Cat()};
addAnimal(cats);
}
and the original method stays the same:
public void addAnimal(Animal[] animals) {
animals[0] = new Dog();public void foo() {
Cat[] cats = {new Cat(), new Cat()};
addAnimal(cats); // no problem, send the Cat[] to the method
}
and the original method stays the same:
public void addAnimal(Animal[] animals) {
animals[0] = new Dog();
}

16

Why passing a subtype as method parameter in arrays is allowed but not allowed in collections?

Because ArrayStoreException will prevent you from putting the wrong type of object into an array. But because of type erasure, JVM does not know type of a collection. So it prevents at compile time. syf 647