History Ĭollection implementations in pre- JDK 1.2 versions of the Java platform included few data structure classes, but did not contain a collections framework. If the code is using Java SE7 or later versions, the developer can instatiate Collection as an ArrayList object by using the diamond operator Ĭollections are generic and hence reified, but arrays are not reified. On the other hand, if the developer instead declared a new instance of a Collection as ArrayList, the Java compiler will (correctly) throw a compile-time exception to indicate that the code is written with incompatible and incorrect type, thus preventing any potential run-time exceptions.The developer can fix the code by instantianting Collection as an ArrayList object. If the developer attempts to add a String to this Long object, the java program will throw an ArrayStoreException. For example, if a developer declares an Object object, and assigns the Object object to the value returned by a new Long instance with a certain capacity, no compile-time exception will be thrown. This can be considered an advantage of generic objects such as Collection when compared to arrays, because under circumstances, using the generic Collection instead of an array prevents run time exceptions by instead throwing a compile-time exception to inform the developer to fix the code. Ĭollections are generic and hence invariant, but arrays are covariant. Instead, Collections can hold wrapper classes such as, , or. ![]() Collections can grow and shrink in size automatically when objects are added or removed.Ĭollections cannot hold primitive data types such as int, long, or double. However, unlike arrays, Collections do not need to be assigned a certain capacity when instantiated. The collections framework provides both interfaces that define various collections and classes that implement them.Ĭollections and arrays are similar in that they both hold references to objects and they can be managed as a group. Īlthough referred to as a framework, it works in a manner of a library. ![]() The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures. Collections in Java class and interface hierarchy Java's class and interface hierarchy
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |