week07 generics Flashcards

1
Q

What purpose do Generics serve in Java?

A

Generics allow the creation of classes and methods that can operate on objects of different types, offering compile-time type safety. This minimizes runtime errors by ensuring type checks are done during compilation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you specify a generic type in Java?

A

Generic types are specified using angle brackets and a placeholder type variable (e.g., ArrayList<T>). This variable is replaced with an actual data type during the generic type invocation step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can Java Generics be used with primitive types?

A

No, Generics cannot be used with primitive types. Wrapper classes must be used instead for primitives (e.g., Integer for int).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why must you specify the data type when using Generics?

A

Specifying the data type prevents the use of raw types, which bypass compile-time type checking. Always use Generics with specific data types to maintain type safety.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are bounded type parameters in Generics?

A

Bounded type parameters restrict the data types that can be used with Generics. They can be limited to classes that extend a particular class or interface using the extends keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do Generics relate to Java inheritance?

A

In Generics, inheritance doesn’t apply as expected. For example, Box<Integer> is not a subclass of Box<Number>. Generic types have their own hierarchy independent of the class hierarchy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are wildcards in Generics, and how are they used?

A

Wildcards (?) represent unknown types in Generics. They can be unbounded, upper bounded (? extends Type), or lower bounded (? super Type), providing flexibility in accepting various types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are some limitations of using Generics in Java?

A

Generics cannot be used with primitive types, create instances of type parameters, declare static fields of type parameters, use casts or instanceof with parameterized types, or create arrays of parameterized types.

  1. プリミティブ型との使用: ジェネリクスは、intやdoubleなどのプリミティブ型には使用できません。代わりに、IntegerやDoubleのようなラッパークラスを使用する必要があります。
  2. 型パラメータのインスタンス化: new T()のように、型パラメータを使って新しいインスタンスを作成することはできません。ジェネリクスの型情報はコンパイル時に消去されるため、実行時に具体的な型が何であるかを知る方法がありません。
  3. 静的フィールドの型パラメータ: 型パラメータを使って静的フィールドを宣言することはできません。ジェネリッククラスの型パラメータはそのインスタンスごとに異なる可能性があるため、クラス全体で一貫した静的フィールドを持つことができません。
  4. 型パラメータのキャストとinstanceof: 型パラメータを使用したキャストやinstanceof演算子の使用は制限されています。これは型情報がコンパイル時に消去されるため、実行時に安全な型チェックを保証できないからです。
  5. パラメータ化された型の配列の作成: ジェネリクスを使用して配列を直接作成することはできません(例: new T[])。これは、ジェネリック型の配列が型安全性を維持できない可能性があるためです。
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does ‘Generic code is parameterized’ mean in Java?

A

When you write code using generics, you use placeholders(like T, E, K, V) for types instead of using actual data types(like int, String). This way, your code can work with any type of data, not just one. It makes your code flexible and safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is generic type invocation step in Java?

A

It is a process where you specify the actual data type for the placeholder used in generic code. When you define a class, method, or interface using generics, you use placeholders to represent types that are not specified until the class or method is invoked.

e.g.
How is works:
1. ArrayList<T>, T is a placeholder for the type of elements in the list.

  1. when you want to create an instance of an ArrayList that hold String objects, you write it as ArrayList<String>. String is the actual data type that replace the placeholder T
How well did you know this?
1
Not at all
2
3
4
5
Perfectly