Home > Java > javaTutorial > body text

Generics in collections in java

高洛峰
Release: 2016-12-01 16:31:43
Original
1344 people have browsed it

import java.util.ArrayList;
/*
* Generics: new features of java jdk1.5.
* Benefits of generics:
* 1. Advance runtime errors to compile time.
* 2. Avoid unnecessary forced type conversion.
*
* Custom method generic: A custom generic is a placeholder for a data type or a data type variable. Generally, T type or E element is used as the placeholder symbol
* The placeholder symbol can be written at will. Naming conventions for identifiers must be followed.
* Format of method generics:
* T: defines a generic type
*
* If you pass in a basic data type: its wrapper class has been used when receiving it. .
* int ----> Integer;
* short ---> Short
* double ---> Double
* float ----> Float
* byte ----> Byte
* boolean --->Boolean
* long ---> Long
* char ---> Character
*

*
*/

public class Demo2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Requirement: Change the elements in a collection from lower case to upper case

/*ArrayList list = new ArrayList< String>(); // Generics: Determine that only objects of a certain data type can be stored in the collection.
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
//list.add(123); //Error reported when running
for(int i = 0;i
System.out.println(list.get(i).toUpperCase());
}*/

String s = test("abc");
Integer i = test(123);


}

public static abc test(abc s){


return s;
}

}


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template