What is Java <>?

Introduction

Hello. It's Kecho. ** Do you guys use Generics? ** ** I learned for the first time today. I mean, I hadn't seen it before, but I finally came across a place to learn.

Scenes of meeting Generics

I think the one I meet most is the list. I think you have made the following declaration.

List<String> list = new ArrayList<>();

I only knew that it was a variable length array. Classes that use this <> are called generic classes.

What kind of role is it?

List<String> list

First of all, this part. List is an interface that holds and handles multiple elements like an array ** in the first place. Not limited to String, you can store objects of various types. (Only one type.) So, the above declaration stipulates that String will be stored this time.


new ArrayList<>();

Next this part. The <> is empty, but I'm just omitting it. (Type can be inferred from the String on the left side)

General generics

Let's touch on generics from a broader perspective, not just the list. The simple definition is as follows.


public class Gene<T> {
	private T t;

	public void setT(T t) {
		this.t = t;
	}

	public T getT() {
		return t;
	}
}

The usage is as follows

Gene<String> g1 = new Gene<>(); //Fixed with String type
Gene<Integer> g2 = new Gene<>(); //Fixed as Integer type
g1.setT("test");
System.out.print(g1.getT()); // test
g2.setT(24);
System.out.print(g2.getT()); // 24

What makes me happy

In this example, we were able to use multiple classes for the same class.

No, isn't this all right?

public class GeneObj {
	Object t;

	public void setT(Object t) {
		this.t = t;
	}

	public Object getT() {
		return t;
	}
}

I tried using the Object class for the part that was limited by type T. Let's use it.

GeneObj g1 = new GeneObj(); 
GeneObj g2 = new GeneObj(); 
g1.setT("test");
System.out.print(g1.getT()); // test
g2.setT(24);
System.out.print(g2.getT()); // 24

You could write it with similar code.

Summary

Do I have to use Generics? ?? ??

No no

Both work in this example, but there is a problem with using Object. Simply put, this class is awkward to use.

Usage example that causes a run-time error


GeneObj g1 = new GeneObj(); //Fixed with String type
g1.setT("1");
Integer res = (Integer) g1.getT() + 1; //Occurrence of ClassCastException
System.out.print(res);

Above, I accidentally packed a String instead of an Integer. Therefore, ClassCastException occurred when casting to Integer class. From the user's point of view, they have to know the type that they setT, which is a hotbed of bugs. ** With Generics, you don't have to use a cast in the first place and you can notice it with a compile error. ** **

Summary

Generics are needed to balance type flexibility with code maintainability

Recommended Posts

What is java
What is Java <>?
What is Java
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Java Assertion? Summary.
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
What is Cubby
What is Docker?
What is null? ]
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Gradle?
What is POJO
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is centOS
What is RubyGem?
What is programming?
What is a lambda expression (Java)
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is a class in Java language (3 /?)
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What is the main method in Java?
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?