This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
What is a primitive type, and why shouldn’t we use it?
What are primitive types in Java, and why do I often hear that I shouldn't use them in new code? If we can't use primitive types, what are the options? What are the better options?Copy the code
Answer:
What are primitive types in Java, and why do I often hear that I shouldn't use them in new code?Copy the code
Primitive types are the ancient history of the Java language. At the beginning of Collections, they Objects had nothing, nothing. Each operation of the required cast on Collections was converted from Object to the desired type.
List aList = new ArrayList(); String s = "Hello World!" ; aList.add(s); String c = (String)aList.get(0);Copy the code
While this works most of the time, errors do occur
List aNumberList = new ArrayList(); String one = "1"; //Number one aNumberList.add(one); Integer iOne = (Integer)aNumberList.get(0); //Insert ClassCastException hereCopy the code
The old untyped collection cannot enforce type safety, so the programmer must remember what he stores in the collection. Generics, invented to overcome this limitation, require the developer to declare the type of storage once and then the compiler to execute it.
List<String> aNumberList = new ArrayList<String>(); aNumberList.add("one"); Integer iOne = aNumberList.get(0); //Compile time error String sOne = aNumberList.get(0); //works fineCopy the code
For comparison:
// Old style collections now known as raw types
List aList = new ArrayList(); //Could contain anything
// New style collections with Generics
List<String> aList = new ArrayList<String>(); //Contains only Strings
Copy the code
The comparison interface is complex:
//raw, not type save can compare with Other classes class MyCompareAble implements CompareAble { int id; public int compareTo(Object other) {return this.id - ((MyCompareAble)other).id; } } //Generic class MyCompareAble implements CompareAble<MyCompareAble> { int id; public int compareTo(MyCompareAble other) {return this.id - other.id; }}Copy the code
Note that it is not possible to implement the CompareAble interface compareTo(MyCompareAble) with primitive types. Why you shouldn’t use them:
Any content Collection stored in Object must be converted before it can be used. Using generics allows compile-time checks to use primitive types and store each value as an ObjectCopy the code
What the compiler does: Generics are backward compatible, using the same Java classes as the original type. Most of the magic happens at compile time.
List<String> someStrings = new ArrayList<String>();
someStrings.add("one");
String one = someStrings.get(0);
Copy the code
Will compile to:
List someStrings = new ArrayList();
someStrings.add("one");
String one = (String)someStrings.get(0);
Copy the code
If you use primitive types directly, this is the same code you wrote. Since I’m not sure what will happen to the CompareAble interface, I think it will create two compareTo functions, one of which takes a and the other MyCompareAble takes anObject and passes it to the first function after the conversion.
What’s the alternative to primitive types: using generics
The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 2…
The authors suggest: Use generics for easy extension
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️