当前位置:网站首页>Do not use primitive types in new code during the use of generic types
Do not use primitive types in new code during the use of generic types
2022-06-22 08:26:00 【ambition_ forever】
public class Generic {
public static void main(String[] args) {
// Original ecological type
List list = new ArrayList<>();
list.add(1);
list.add(2L);
System.out.println(list.get(0));
// System.out.println((Integer) list.get(1));
// Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
// at com.atguigu.springbootbase.effective.E01.main(E01.java:22)
// Add generics
List<Integer> list2 = new ArrayList<>();
/* list2.add(1);
// Required type:Integer Provided:long
list2.add(2L);*/
// Generics are removed when passing parameters
odd(list2, 1);
oddNew(list2, 3);
odd(list2, 2L);
// oddNew(list2, 2L); // Report an error in advance , Deal with in advance
oddNew(list2, 4); // Report an error in advance , Deal with in advance
System.out.println(list2);
// Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
// at com.atguigu.springbootbase.effective.E01.main(E01.java:37)
// System.out.println((Integer) list2.get(1));
System.out.println((Integer) list2.get(3));
}
// Generics are removed when passing parameters
public static void odd(List list, Object o) {
list.add(o);
}
// Add generics when passing parameters
public static void oddNew(List<Integer> list, Integer o) {
list.add(o);
}
}
边栏推荐
- Distributed transaction
- Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
- Three concurrent features 1- visibility
- The jdbcurl is configured correctly in the project, but the jdbcurl is the wrong path after the project is started
- Seven challenges faced by CIO in 2022 and Solutions
- Restrict input type (multiple methods)
- PostgreSQL source code (56) extensible type analysis expandedobject/expandedrecord
- Questions 1 to 100 of the national information security grade examination nisp level 1 question bank (1)
- Failed to access the ES installed on Tencent ECs, but the solution to accessing the ES successfully on the server
- C language implements inserting and reading pictures into MySQL
猜你喜欢

Failed to access the ES installed on Tencent ECs, but the solution to accessing the ES successfully on the server

Some mathematical operation functions in LabVIEW

Example of QT qtableview

并发三大特性2-有序性

How to troubleshoot OOM

并发三大特性1-可见性

一文彻底搞懂My SQL索引知识点

QT error prompt 1:invalid use of incomplete type '***‘

Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security

多点闹钟实例
随机推荐
Learn data warehouse together - Zero
The necessity of steam education culture inheritance
Oracle database pl/sql procedure body, cursor, stored procedure
How to create an index
Third party services (file and picture storage)
MySQL queries data within one hour
Chmod Chmod command
Optimization of MySQL paging scheme by database and table
Chapter VIII web project testing (the end of this chapter)
Alibaba cloud ~ simply send SMS
On Fresnel phenomenon
Summary of sub database and sub table 1
每周推荐短视频:什么是“计算世界”?
Bee framework, an ORM framework
Multiple ways for idea to package jars
成为一名卓越云架构师要具备的五项技能
Carry out effective maker education courses and activities
Postgresql源码(56)可扩展类型分析ExpandedObject/ExpandedRecord
QT 自定义组合控件(类提升功能)
Five skills to be an outstanding cloud architect