目錄
Java 中泛型是如何實現的?
理解 Java 中的泛型
如何透過泛型實作程式碼可重複使用?
如何使用泛型實現型別安全?
Using ArrayList with Generics:
How do Generics in Java make Work Easier?
What else to do with Generics in Java?
1. Bounded Type
2. Type Wildcards
Advantages of Generics in Java
Generics in Java Skills
Why use Generics in Java?
Scope for Generics in Java
Conclusion
首頁 Java java教程 Java 中的泛型是什麼?

Java 中的泛型是什麼?

Aug 30, 2024 pm 04:18 PM
java

Java 中的泛型是一項進階功能,可實現程式碼可重複使用性和類型安全性。程式碼可重用功能是透過定義通用類別、介面、建構函數和方法來實現的。泛型使用資料類型聲明來確保類型安全,從而消除運行時錯誤的機會。尖括號“”符號用於實現泛型,類型參數在括號內定義。這些類型參數包括“T”(類型)、“E”(元素)、“N”(數字)、“K”(鍵)和“V”(值)。具有類型 T 參數的泛型類別的範例是「public class DemoGenericClass」。 {…}’

 

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java 中泛型是如何實現的?

泛型是使用尖括號「」實現的括號內包含型別參數「T」。例如,在中,類型參數「T」是一個佔位符,表示將在運行時分配資料類型。

泛型類別可以定義為:

代碼:

public class MyGenericClass<T> {…}
登入後複製

以下是標準型別參數:

  • T: 類型
  • E: 元素
  • N: 數字
  • K:鑰匙
  • V:

多參數的情況下,用S、U、V等分別定義第二、三、四參數。

理解 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」

但是,只有 String 可以呼叫此方法。嘗試傳遞任何其他資料類型(例如整數或布林值)將導致編譯時錯誤,指出「Runner 類型中的方法welcome(String) 不適用於參數(布林值)」

輸出:

Java 中的泛型是什麼?

如果想要為不同的資料型別呼叫類似的方法,可以建立一個接受所需資料型別作為參數的新方法。這種以不同資料類型的參數重寫方法​​的技術稱為「方法重載」。然而,這種方法的一個缺點是它可能會導致更大的程式碼大小。

也可以使用泛型重寫上述方法,並將其用於我們需要的任何資料類型。

定義通用方法:

代碼:

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 中的泛型是什麼?

如何使用泛型實現型別安全?

數組和集合之間的主要區別之一是數組只能存儲同質數據,而集合可以存儲異質數據。換句話說,集合可以儲存任何類型的對象,包括使用者定義的資料類型。

注意:集合只能保存對象,包括使用者定義的資料類型,而不是原始資料類型。為了使用原始資料類型,集合使用包裝類別。

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1657
14
CakePHP 教程
1415
52
Laravel 教程
1309
25
PHP教程
1257
29
C# 教程
1229
24
突破或從Java 8流返回? 突破或從Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP與Python:了解差異 PHP與Python:了解差異 Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP與Python:核心功能 PHP與Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

Java程序查找膠囊的體積 Java程序查找膠囊的體積 Feb 07, 2025 am 11:37 AM

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP:許多網站的基礎 PHP:許多網站的基礎 Apr 13, 2025 am 12:07 AM

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

See all articles