首页 > 编程语言 >Java generic cheatsheet(jrebel.com)

Java generic cheatsheet(jrebel.com)

时间:2022-09-22 22:22:05浏览次数:76  
标签:code Java Generics generic cheatsheet generics jrebel type

摘自 https://www.jrebel.com/blog/java-generics-cheat-sheet

pdf 版 https://www.jrebel.com/system/files/java-generics-cheat-sheet.pdf

 

 

December 1, 2016

Java Generics Cheat Sheet

Java Application Development

This post continues our series of one-page printable cheat sheets about Java and related technologies that we’ve been producing for almost a year now. Today it’s all about Java generics, a Java 5 language feature that helps to decrease verbosity in The feature was added to Java 10 years ago, and even today it still confuses many Java developers.

In this article, we'll give some background on Java Generics, look at wildcards, methods, and PECs. Then, at the end of the article, we'll share our free, one-page Java generics cheat sheet pdf.

Speed Up Your Development

JRebel can save you hours of development time. Try it free for 10 days with a JRebel trial.

TRY JREBEL FOR FREE

What Are Java Generics?

Java Generics allow developers to implement a collection of types in a single declaration, reducing the verbosity of your code as well as its cost of maintenance.

Java Generics were added to the now ancient Java 5 way back in 2005 when the world was a much simpler place. The main idea of generics was to enhance the Java compiler by using additional information on the classes for additional type safety. The most prominent user of generics is, perhaps, the Java Collections framework, which consists of the classes that act as containers for other objects. For those speaking in C terminology, Java Generics are similar to "templates" in C.

Using Java Generics

Using Java Generics, a class can be parameterized with a type argument. For instance, consider class A, in the example below.


    class A {
    ...
    }


You can make it a generic type by declaring it as A (pronounced A of T). Now, you can use the type variable T, in the body of class A, as if it were a proper type.


    class A { 
    T myFieldOfTypeT;
    ...
    }


To instantiate A, you need to provide the arguments for the type parameters, which is the actual type you want to use:


    A a = new A(); 


Now a is an instance of the A class, which is a proper parameterized generic class. Here we need to clarify a few definitions:

  • Generic Type - A generic type is a class that is parameterized with type arguments, for example, ArrayList<T>.
  • Parameterized Type -  A parameterized type is a class where the type parameter is instantiated with a fixed argument, for example, ArrayList<Long>.
  • Raw Type - Araw type is a generic type that is not parameterized with anything, like new ArrayList(). 

But, why would you use a raw class? Well, in a nutshell, there are no benefits of using raw classes whatsoever. If you’re to take away one thing about the generics, here’s the most important bit: Generics do not exist at runtime! Generics are compile-time only. It means they are a utility for you to have a more readable code.

What Technologies Are Java Developers Using in 2020?

Our latest Java Productivity Report gives adoption stats on technology, application architecture and more. Download today and get immediate insights into the Java ecosystem.

GET THE REPORT

Java Generics Wildcard

You will often find a ‘?’ Symbol inside a generic parameter. It is a wildcard and stands for an arbitrary type. For example, an ArrayList<?> is an array list of any type. It means that it can represent an ArrayList of Strings, or an ArrayList of Integers, or whatever type. However, at the runtime, it will have some fixed type.

You might wonder what’s the difference between declaring your variable as List<Object> versus List<?>? In a nutshell, Collection<Object> is heterogeneous collection. It is parameterized with the common superclass of all classes in Java: java.util.Object. It means that this collection can take any object, and if you get an object from it, you cannot expect it to be anything more specific than an instance of the Object class.

Java Generics Wildcard
Wildcard Collection TypeDescription
Collection<Object> Any object goes in.
Collection<?> Homogenous collection of arbitrary type.

On the other hand, Collection<?> is a homogenous collection of arbitrary type. It means that at some point of time the compiler will figure out the bound for the types itself. So if the situation arises, we’ll get a Collection<Set> or Collection<CharSequence>, but it will be parameterized specifically. So it’s a very generic way of writing code.

In general, you can safely avoid wildcards in the generics, until you understand what it is doing to your code. Most of the time you’ll be completely fine without them.

Method Overloading and Overriding

When you use generics with overloaded methods, you may be surprised at what you see. Imagine the code below. What do you think the return value will be if you call generic(“hello world”)?


    String f(Object s) { 
      return "object";
    } 
    String f(String s) { 
      return "string";
    } 
     void generic(T t) { 
      f(t); 
    } 


It can appear unreasonable, but the returned value will be "object.” The reason for that is that generics do not exist at runtime. It’s a compile time concept, and the compiler will generate just a single implementation of the generic method. And since the code has to be ready to accept Object as a parameter, it will only generate code that calls f(Object s).

The situation with overriding methods is often even trickier. So be prepared to dive into the implementation details and peek at the generated bytecode to better understand the behavior.

Producer Extends Consumer Super (PECS)

Take a look at the centerpiece of the cheat sheet, the PECS image. PECS stands for Producer Extends Consumer Super, and it’s a nice rule of thumb for designing an API that uses generics. java-generics-cheat-sheet-graphic-v1

The image is a reworked illustration by Andrey Tyukin available under the CC-BY-SA.

First of all, there are two keywords used for type parameters. The extends keyword, in the <T extends X> declaration means that T is restricted to the subtypes of X, including X itself. The super means the opposite: <T super X> restricts T to be X or a superclass of X.

For the explanation of the PECS concept, look at the signature of the Collections.sort method.


Collections.copy(List<? super T> dest, List<? extends T> src)


The copy methods copies (duh!) elements from the src list to the dest list. The src list produces elements of type T or subtypes of T. The dest list accept elements, of type T or supertypes of T.

Generic Collections and Designing a Java API 

When you’re designing your own API, think about how you want to use generic collections in your code. If you’re retrieving elements from it, parameterize your method arguments using <? extends T>. It will make your code more inclusive, as the method will be usable not just by collections of T, but by its subtypes too. On the other hand, if you intend to put the elements into the collection, parameterize it with <? super T>, so someone can pass you any collection that accepts T: a collection of T, or a collection of Objects, for example.

If you plan on using the collection for both types of operations: consuming objects and producing them, it becomes harder. You probably need to just parameterize it as <T>, but then again maybe you want to rethink your API altogether. If you have a hierarchy of types, the collections of these types follow the hierarchy when they are producers, and the reverse when the hierarchy are consumers.

In the cheat sheet, we also mention recursive generics declarations. You can use this neat trick to add additional constraints on your type parameters to allow the compiler to infer more information about the types in your code.

Before we go, let’s reiterate the most important point you have to keep in mind about the generics: Java generics do not exist at runtime, it’s a compile-time concept. The main idea is to allow you write more general, less verbose code that is easier to read and maintain.

Additional Resources

Want to learn more about how Java is poised for changes? This webinar looks at features in the works for JDK 15, as well as projections for features going forward.

 

Perhaps the most complete and advanced resource about Java Generics is the FAQ by Angelika Langer. It’s not a tutorial, but it answers almost all the questions you might have about Java generics. Or you can go through the Oracle tutorial about Java Generics.

Both are quite long, so if you prefer video Richard Warburton and Raoul-Gabriel Urma have a great session about Java Generics, their past, present and future development.

We also have a lot of other Java cheat sheets and guides that you should check out:

Download the Java Generics Cheat Sheet PDF

In this cheat sheet, we looked at Java generics, including their use cases, rules of thumb, and best practices. We by no means think that a one-page cheat sheet with a blog accompanying it can explain all of the details you need to master generics, but it’s a nice starting point.

标签:code,Java,Generics,generic,cheatsheet,generics,jrebel,type
From: https://www.cnblogs.com/harrychinese/p/java_generic.html

相关文章

  • Eclipse 快捷键(jrebel.com)
    https://www.jrebel.com/sites/rebel/files/pdfs/cheat-sheet-rebel-eclipse-keyboard-shortcuts.pdf ......
  • Spring annotation(jrebel.com)
    摘自https://www.jrebel.com/blog/spring-annotations-cheat-sheetAugust5,2021SpringAnnotationsCheatSheetJavaFrameworksDeveloperProductivityWe'veg......
  • C#教程 - 泛型(Generic Types)
    更新记录转载请注明出处:https://www.cnblogs.com/cqpanda/p/16690994.html2022年9月18日发布。2022年9月10日从笔记迁移到博客。泛型(GenericTypes)说明一般情况下......
  • 因势而变,因时而动,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang泛型(generic)的使用E
    事实上,泛型才是Golang1.18最具特色的所在,但为什么我们一定要拖到后面才去探讨泛型?类比的话,我们可以想象一下给小学一年级的学生讲王勃的千古名篇《滕王阁序》,小学生有多大......
  • Idea热部署插件JRebel的安装与使用
    1、背景一般更新了Java文件后要手动重启整个项目,才能生效,这很影响开发速度,于是热部署就出现了。热部署就是正在运行状态的应用,修改了他的源码之后,在不重新启动的情况下......
  • jenkins pipeline 中使用Generic Webhook Trigger插件
    GenericWebhookTrigger插件在pipeline中的使用1.pipeline中的配置和图形界面的配置应该保持一致,具体的做法是1.1编写pipeline pipeline{agentanytr......
  • CollectionAndGeneric
    集合:集合分为单列集合和双列集合,一个是存储单个数据,一个是存储数据和对应的key值集合和数组的相异点:1.集合相较于数组更加灵活,数组的长度一旦创建就是固定的而集......