Javaのジェネリックとは何ですか?

WBOY
リリース: 2024-08-30 16:18:36
オリジナル
847 人が閲覧しました

Java のジェネリックスは、コードの再利用性とタイプ セーフを可能にする高度な機能です。コードの再利用機能は、ジェネリック クラス、インターフェイス、コンストラクター、およびメソッドを定義することによって実現されます。ジェネリックスはデータ型宣言を使用して型の安全性を確保し、それによって実行時エラーの可能性を排除します。山かっこ「<>」記号はジェネリックの実装に使用され、型パラメーターはかっこ内で定義されます。これらの型パラメーターには、型を表す「T」、要素を表す「E」、数値を表す「N」、キーを表す「K」、値を表す「V」が含まれます。 Type T パラメータを持つジェネリック クラスの例は、「public class DemoGenericClass」です。 {…}'

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Java でジェネリックスを実装する方法?

ジェネリックは山括弧「<>」を使用して実装されます。括弧で型パラメータ「T」を囲みます。たとえば、 の型パラメーター「T」はプレースホルダーであり、データ型が実行時に割り当てられることを示します。

ジェネリック クラスは次のように定義できます:

コード:

public class MyGenericClass<T> {…}
ログイン後にコピー

以下は標準タイプのパラメータです:

  • T: タイプ
  • E: 要素
  • N: 番号
  • K: キー
  • V:

マルチパラメータの場合、S、U、V などを使用して、それぞれ 2 番目、3 番目、4 番目のパラメータを定義します。

Java のジェネリックスを理解する

タイプ セーフティとは何ですか?また、どのように機能しますか?ジェネリック クラス、インターフェイス、コンストラクター、メソッドは、再利用可能にする通常のクラスやメソッドとどのように異なりますか?

静的型付け言語である Java では、変数を使用する前にそのデータ型を宣言する必要があります。

例:

コード:

String myString ="eduCBA";
ログイン後にコピー

上記のコードでは、「String」はデータ型で、「myString」は String 型の値を保持する変数です。

ここで、以下のように文字列の代わりにブール値を渡そうとすると:

コード:

String myBooleanStr = true;
ログイン後にコピー

すぐに「型が一致しません: ブール値から文字列に変換できません」というコンパイル時エラーが発生します

出力:

Javaのジェネリックとは何ですか?

ジェネリックを使用してコードの再利用性を実現するにはどうすればよいですか?

次に、通常のメソッドを定義しましょう:

コード:

public static void welcome(String name){
System.out.println("welcome to " + name);
}
ログイン後にコピー

このメソッドは、文字列パラメータを渡すことによってのみ呼び出すことができます。

コード:

welcome("eduCBA");
ログイン後にコピー

その出力は「eduCBA へようこそ」となります

ただし、このメソッドを呼び出せるのは文字列のみです。整数やブール値など、他のデータ型を渡そうとすると、「タイプ Runner のメソッド welcome(String) は引数 (ブール値) には適用できません」というコンパイル時エラーが発生します

出力:

Javaのジェネリックとは何ですか?

別のデータ型に対して同様のメソッドを呼び出したい場合は、必要なデータ型をパラメータとして受け入れる新しいメソッドを作成できます。異なるデータ型のパラメーターを使用してメソッドを書き換えるこの手法は、「メソッドのオーバーロード」と呼ばれます。ただし、このアプローチの欠点の 1 つは、コード サイズが大きくなる可能性があることです。

ジェネリックを使用して上記のメソッドを書き換え、必要なデータ型に使用することもできます。

ジェネリック メソッドの定義:

コード:

public static <T> void welcome(T t){
System.out.println("it is " + t);
}
ログイン後にコピー

注: ここで、「t」は T 型のオブジェクトです。メソッドの呼び出しに使用される実際のデータ型は、型パラメーター「T」に割り当てられます。

これにより、必要に応じて、文字列、ブール値、整数などを含むさまざまなデータ型でメソッドを再利用できます。

コード:

welcome("educba");
Integer myint = 1;
welcome(myint);
welcome(true);
ログイン後にコピー

上記のステートメントは次の出力を提供します:

出力:

it is educba
it is 1
it is true
ログイン後にコピー

したがって、ここでジェネリックを使用すると、さまざまなデータ型に対してメソッドを再利用できます。

Javaのジェネリックとは何ですか?

ジェネリックを使用して型安全性を実現するには?

配列とコレクションの主な違いの 1 つは、配列は同種のデータのみを保存できるのに対し、コレクションは異種データを保存できることです。つまり、コレクションには、ユーザー定義のデータ型を含むあらゆる種類のオブジェクトを格納できます。

注: コレクションは、ユーザー定義のデータ型を含むオブジェクトのみを保持でき、プリミティブ データ型は保持できません。プリミティブ データ型を操作するために、コレクションはラッパー クラスを利用します。

Now, let’s consider an ArrayList.

Code:

ArrayList myList = new ArrayList();
ログイン後にコピー

One can add elements of various data types, such as strings, integers, and doubles, to an ArrayList object.

Code:

myList.add("eduCBA");
myList.add(1);
myList.add(5.2);
ログイン後にコピー

On printing the ArrayList object, one can see that it contains the following values: [eduCBA, 1, 5.2].

Output:

Javaのジェネリックとは何ですか?

To retrieve these values into variables, one needs to typecast them.

Code:

String someStr = (String)myList.get(0);
Integer someInt = (Integer)myList.get(1);
Double someFlt = (Double)myList.get(2);
ログイン後にコピー

If one does not typecast, it will prompt a compile-time error stating, “Type mismatch: cannot convert from Object to String”

Output:

Javaのジェネリックとは何ですか?

Thus, one must typecast them to their respective types while retrieving the objects from the ArrayList. However, in real-time scenarios, an ArrayList can contain thousands of records, and manually typecasting every object may not be feasible. There is the risk of mistakenly typecasting an object to an incorrect data type. In such cases, a runtime error will occur, stating “Exception in thread “main” java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.serviceClasess.Runner.main(Runner.java:43)”.

Javaのジェネリックとは何ですか?

As there is no guarantee with regard to the type of data present inside a collection (in this case, ArrayList), they are considered unsafe to use with respect to type. Here, Generics play a role in providing type safety.

Using ArrayList with Generics:

Code:

ArrayList<String> myList = new ArrayList<String>();
ログイン後にコピー

The String type is specified inside the angular brackets “>” which means that this particular implementation of ArrayList can only hold String type data. If one tries to add another data type, it will simply throw a compile-time error. Here, the ArrayList has been made type-safe by eliminating its chances of adding a data type other than “String.”

Output:

Javaのジェネリックとは何ですか?

Now, since one has specified the data type that is allowed to be added to the collection with the help of Generics, there is no need to typecast it while retrieving the data. One can simply retrieve the data by writing.

Code:

String someStr = myList.get(0);
ログイン後にコピー

Output:

Javaのジェネリックとは何ですか?

How do Generics in Java make Work Easier?

  • It helps make the collections type safe and ensures the code does not fail at a later point due to any run time exception.
  • It saves the coder from having to typecast each object in the collection, which simplifies and speeds up the code development process.
  • Generics allow writing code in a way that can work with multiple data types.

What else to do with Generics in Java?

So far, we have seen how we can achieve type safety and code reusability with Generics.

In addition to type safety and code reusability, here are some other features that Generics can provide:

  • Bounded & Multiple Bounded Types
  • Type Wildcards

1. Bounded Type

In the case of a bounded type, the data type of a parameter is bounded to a particular range. The keyword “extends” helps achieve this.

For example, let’s consider a Generic class with a bounded type parameter that extends the ‘Runnable interface’:

Code:

class myGenericClass<T extends Runnable>{}
ログイン後にコピー

Now, while creating its object in another class:

Code:

myGenericClass<Thread> myGen = new myGenericClass<Thread>();
ログイン後にコピー

The above statement will execute perfectly without any errors. In the case of the bounded type, one can pass the same class type or its child class type. Also, one can bind the parameter type to an interface and pass its implementations when invoking it, as in the example above.

What happens if one uses any other type of parameter?

Code:

myGenericClass<Integer> myGen = new myGenericClass<Integer >();
ログイン後にコピー

In the above case, it will result in a compile-time error, stating “Bound mismatch: The type Integer is not a valid substitute for the typecast of the type myGenericClass

Output:

Javaのジェネリックとは何ですか?

  • Multiple bounded types: In the case of multiple bounded types, one can bind the parameter data type to more than one type.

Example:

Code:

class myGeneric<T extends Number & Runnable>{}
ログイン後にコピー

In this case, one can pass any type that extends the Number class and implements the Runnable interface. However, when using multiple bounded types, a few things should be noted:

  • One cannot extend more than one class at a time.
  • One can extend any number of interfaces simultaneously, i.e., there is no limit for interfaces.
  • The class name should always come first, followed by the interface name. If not, it will result in a compile-time error.

2. Type Wildcards

The “?” (question mark) symbol represents Type Wildcards. It makes use of two main keywords:

  1. extends (to define upper bound)
  2. super (to define lower bounds).

Example:

Code:

ArrayList<? extends T> al
ログイン後にコピー

The ArrayList object “al” will hold any data of type T and all its subclasses.

Code:

ArrayList<? super T> al
ログイン後にコピー

The ArrayList object “al” will hold any data of type T and all its superclasses.

Advantages of Generics in Java

  • Flexibility: Generics allow the code to accommodate different data types with the help of Generic classes and methods.
  • Code Maintenance and Reusability: Due to Generic classes and methods, one need not rewrite the code in case of a change in requirements later, making the code easier to maintain and reuse.
  • Type Safety: Provides type safety to the collection framework by defining the data type the collection can hold beforehand; and eliminating any chances of failure at run time due to ClassCastException.
  • Eliminating the Need to Typecast: Since the data types being held by the collections are already determined, one need not typecast it at the time of retrieval. This reduces the code’s length and a coder’s effort.

Generics in Java Skills

  • To work with Generics, one should be proficient in the basics of Java.
  • One should understand how type checking and typecasting work. Thorough knowledge of other concepts such as method overloading, the relationship between parent and child classes, interfaces, and their implementations is necessary.
  • Also, it is crucial to understand the difference between primitive data types (system-defined data type) and objects (user-defined data type) when working with the collection framework.

Why use Generics in Java?

  • Using Generics makes the code more maintainable as it reduces the need to rewrite data type-specific code every time there is a change in requirements.
  • By using Generics bounded type, one can restrict the data type and, at the same time, provide flexibility to the code by defining its range.
  • Generics provide type safety, making the code less error-prone and less likely to fail at a later point.

Scope for Generics in Java

Generics scope is limited to compile time, i.e., the Generics concept is applicable only at compile time but not at run time.

Example:

Code:

ArrayList myList = new ArrayList<Integer>();
ArrayList myList = new ArrayList<Float>();
ArrayList myList = new ArrayList<Double>();
ArrayList myList = new ArrayList<Boolean>();
ログイン後にコピー

Here all the above four statements are the same. They will allow adding any type of data to the list object.

Conclusion

Generics renders coding easy for a coder. It diminishes the chances of encountering ClassCastException at run time by providing strong type-checking. Also, it eliminates the need for typecasting, which means less code needs to be written. It allows the development of Generic algorithms independent of the data type they are working with.

以上がJavaのジェネリックとは何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!