A Java user over a dozen years ago tried to study the functions of Java8 (Generics).

Introduction

Recently, I was blessed with the opportunity to use Java because of my work. I updated my ancient Java (1.3) knowledge.

What are Generics?

This function determines the processing of the type to be used at the time of implementation.

This alone is refreshing, so I would like to explain it with an example.

Compare the presence or absence of generics with code

Code around Java 1.3

List list = new ArrayList();

//Store value in list
// String ->Since it is a conversion to Object, it can be stored implicitly.
list.add("1");
list.add("2");
list.add("3");

for (Iterator it = list.iterator(); it.hasNext(); ) {
	//Get and display the value of list
	//Object at the time of acquisition->String needs an explicit cast
	String str = (String) it.next();
	System.out.println(str);
}

Java 8 code

//Prepare ArrayList that uses String type
List<String> list = new ArrayList<String>();
// List<String> list = new ArrayList<>(); //Since the type is fixed when the variable is declared, when creating an instance<>Can be omitted


//Store value in list
//String can be stored as it is
list.add("1");
list.add("2");
list.add("3");

for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
	//Get and display the value of list
	//No need to cast because it can be obtained as a String
	String str = it.next();
	System.out.println(str);
}

What is good to use?

In Java1.3, when using collections I had to store it as an Object type. Of course, when extracting it, it is also an Object type, so I had to cast it according to the type used.

The songwriter is to cast when this is taken out If you store the wrong type, you will get a ClassCastException. Of course, it's an Exception, so you won't notice the mistake until you run it.

On the other hand, if you use Generics, you can determine the type at the time of implementation. Wrong type to store = Compile error occurred It is also a function that helps reduce easy mistakes.

Recommended Posts

A Java user over a dozen years ago tried to study the functions of Java8 (Generics).
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
I tried to summarize the basics of kotlin and java
I tried to make a client of RESAS-API in Java
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
A story that confirmed the profile of Yasuko Sawaguchi 36 years ago
How to check for the contents of a java fixed-length string
A quick look back at Java over the last five years
The story of forgetting to close a file in Java and failing
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
Output of the book "Introduction to Java"
What I tried when I wanted to get all the fields of a bean
How to get the ID of a user authenticated with Firebase in Swift
Validate the identity token of a user authenticated with AWS Cognito in Java
[Small story] I tried to make the java ArrayList a little more convenient
Make a margin to the left of the TextField
java I tried to break a simple block
Measure the size of a folder in Java
I tried to develop a DUO3.0 study website.
Set the time of LocalDateTime to a specific time
[Java] How to get the authority of the folder
I tried to break a block with java (1)
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
Java Welcome to the Swamp of 2D Arrays
I tried to create a log reproduction script at the time of apt install
A memo about the types of Java O/R mappers and how to select them
I tried to operate home appliances by holding a smartphone over the NFC tag
[Java] How to get the URL of the transition source
How to write Scala from the perspective of Java
I tried to summarize the state transition of docker
I tried to decorate the simple calendar a little
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
[Java] How to get the maximum value of HashMap
I tried to create a Clova skill in Java
I tried to make a login function in Java
[Java] When writing the source ... A memorandum of understanding ①
Java: Use Stream to sort the contents of the collection
I tried to implement the Euclidean algorithm in Java
A survey of the Kubernetes native Java framework Quarkus
I finished watching The Rose of Versailles, so I tried to reproduce the ending song in Java
Let's take a look at the functions of Keycloak's management console (user edition), user account service
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.