当前位置:网站首页>Too beautiful promise because too young
Too beautiful promise because too young
2022-06-25 07:07:00 【grace. free】
Have you promised anything , And paid the price for these promises .
For example, you promise your parents , Take a daughter-in-law home for the new year , To find a wife , You are so tired .
For example, you promise your partner , I will do all the housework for you in the future , As a result, you wipe the floor every day 、 Brush bowl 、 Cooking, etc .
Some promises are made because they are too young , For example, I will marry you when I grow up .
Some promises are made to achieve an end , For example, I will treat you well .
Some promises are made to keep their jobs , For example, it can definitely be completed on time .
We're familiar with Java, Once made a promise , That's it " Binary backward compatibility ", This commitment is clearly written in 《Java language norm 》 Medium .
that “ Binary backward compatibility ” What does that mean , That is to say, for example, you are JDK1.2 Compile a Class file , It must be ensured that this document is in JDK999 Or the newer version can work normally .
Why is there such a commitment , I think the probability is to make Java Users can safely and boldly code , Do not consider a series of incompatibilities caused by the version upgrade , To put it bluntly , It is to attract users .
We mentioned earlier , Promise something , It is bound to pay a price . that Java Have you ever paid a price for this ?
The answer is : yes , we have .
One 、 Commitments and costs
We all know 2004 year , That is, when I was still playing with mud ,Java Updated a version , namely Java5.0.
In this version Java Added generic syntax features .
Java The generic implementation chosen is “ Type Erasure ”, What is type erased , That is to say , All generics are replaced with the original bare types , And the cast is inserted in the corresponding place , such as ArrayList 、ArrayList After compilation, it is actually the same type ArrayList , It can be understood that the type is erased .
Noun description :" Bare type " It can be considered the common parent type of all generic instances of this type
such as :ArrayList 、ArrayList 、ArrayList The bare type of is ArrayList
We can look at the following code :
package ss;
import java.util.ArrayList;
/** * @author Muzi's day and night programming */
public class AA {
public static void main(String[] args) {
ArrayList<String> sarr = new ArrayList<>();
ArrayList<Integer> iarr = new ArrayList<>();
// This is the bare type That is, there are no generic modifiers
// It's like not wearing clothes
ArrayList arrayList;
arrayList = sarr;
arrayList = iarr;
}
}
Why is this Java The price paid ?
We cannot use basic types as generics
Because after the generic type is erased , Where it is used, it uses a cast , That is from Object Convert to basic type , We know that basic types cannot be forcibly transformed, for exampleObject o = (Object)1;In theory, this kind of forced conversion should report an error , because Object Not a parent or base class of a basic type . Someone must have said , I wrote like this , There is no error in the code , That's because the compiler does something , Automatically converted to
Object o = (Object)Integer.valueOf(1);Java The solution given by the author of is very simple , That is, since there is no way to convert , Then I simply don't support generics of native types .
Good! , You have now decided ? Why is the author so straightforward ( It may have been a long time ) What is the decision? , because .. Java It can be packed automatically 、 Unpacking . And that leads to this , Where we use generics, boxing is involved 、 Unpacking , This is also an important reason why generics are slow .
Cannot get generic type information at run time .
What does this mean ,Java Generic types are erased , During operation , We can't get the real type of the generic , If we need to create objects with this type , We need to add one more parameter , for example :package ss; import java.util.ArrayList; import java.util.List; /** * @author Muzi's day and night programming */ public class AA<T> { public static void main(String[] args) { // new AA<String>().test(new ArrayList<>(), String.class); } // We need an extra parameter classType To pass generic types public void test(List<T> list, Class<T> classType) { } }But again C# in , His generics are “ Materialized generics ”, He can support basic types , To put it bluntly, what is your generic type , What will be recorded after compilation , Will not be lost . For example, the following code , For learning C# For people who , They can hardly imagine , stay Java These grammars are illegal in .
/** * @author Muzi's day and night programming */ public class AA<T> { public void test(Object obj) { if (obj instanceof T) { } T newObj = new T(); T[] arr = new T[10]; } }
Two 、 Looking forward to the future
2014 Year is Java generic 10 birthday , This year Oracle Established a system called Valhalla Language improvement project for , Hope to improve Java The defects left by language , Of course, the defect of generics is one of the main goals of this project .
As Java Users of , I firmly believe , The language will move in a better direction .
边栏推荐
- Coffee script unmatched outent error
- In a single-page app, what is the right way to deal with wrong URLs (404 errors)?
- Design of PWM breathing lamp based on FPGA
- Usage of STL map
- mysql 表查询json数据
- Pratique de gestion hiérarchique basée sur kubesphere
- Message queue table structure for storing message data
- What is the real future of hardware engineers?
- ACWING/2004. 錯字
- 有了 MySQL 为什么要用 NoSQL?
猜你喜欢

【xxl-job】池塘水绿风微暖,记得玉真初见面

ES 终于可以搜到”悟空哥“了!

Comparison test of mono 120W high power class D power amplifier chip cs8683-tpa3116

了解zbrush雕刻软件,以及游戏建模的分析

Acwing / 2004. Mauvaise écriture

Uncaught TypeError: Cannot read properties of undefined (reading ‘prototype‘)

Entry level use of flask

分布式锁中的王者方案 - Redisson

有了 MySQL 为什么要用 NoSQL?

深入解析 Apache BookKeeper 系列:第三篇——读取原理
随机推荐
【一起上水硕系列】Day 5
[he doesn't mention love, but every word is love]
Unity获取资源路径
Grouped uitableview has 20px of extra padding at the bottom
Ht8513 single lithium battery power supply with built-in Dynamic Synchronous Boost 5W mono audio power amplifier IC solution
破万,我用了六年!
Cs8126t 3.1w mono ultra low EMI unfiltered class D audio power amplifier IC
Americo technology launches professional desktop video editing solution
Direct select sort and quick sort
Ht7180 3.7V L 12v/2a built in MOS high current boost IC solution
Navicat prevent new query from being deleted by mistake
Three laws of go reflection
使用OpenGL绘制shp文件
Derivation of sin (a+b) =sina*cosb+sinb*cosa
Changing the background color of tab bar - changing the background color of tab bar
How to record a database [closed] - how to document a database [closed]
Derivation of COS (a+b) =cosa*cosb-sina*sinb
Efficient exploration | an application practice of ES geographical location query
Event registration Apache pulsar x kubesphere online meetup hot registration
Derivation of sin (a-b) =sina*cosb-sinb*cosa