JDK 6260652 Bug

Source: Internet
Author: User

Recently looking at the source of the JDK: Copyonwritearraylist.java and Arraylist.java, these 2 classes of constructors, comments in a sentence can not understand.

1   PublicArrayList (collection<?extendsE>c) {2Elementdata =C.toarray ();3Size =elementdata.length;4     //C.toarray might (incorrectly) not return object[] (see 6260652)5     if(Elementdata.getclass ()! = object[].class)6Elementdata = arrays.copyof (elementdata, size, object[].class);7  }8 9   PublicCopyonwritearraylist (collection<?extendsE>c) {TenObject[] elements =C.toarray (); One     //C.toarray might (incorrectly) not return object[] (see 6260652) A     if(Elements.getclass ()! = object[].class) -elements = arrays.copyof (elements, Elements.length, object[].class); - SetArray (elements); the}

Surf the internet to check the information, just know see 6260652 this number represents the number in the JDK Bug library. can go to the official website to view the bug details

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6260652

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6515694

6260652 and 6515694 of these 2 bugs seem to be the same problem. What does this bug mean? Let's take a look at some of the test code:

1      Public Static voidtest1 ()2     {3Subclass[] Subarray = {NewSubclass (),Newsubclass ()};4 System.out.println (Subarray.getclass ());5 6         //class [Lcollection.subclass;7baseclass[] Basearray =Subarray;8 System.out.println (Basearray.getclass ());9 Ten         //java.lang.ArrayStoreException OneBasearray[0] =NewBaseClass (); A     } -  -      Public Static voidtest2 () the     { -list<string> list = Arrays.aslist ("abc"); -  -         //class Java.util.arrays$arraylist + System.out.println (List.getclass ()); -  +         //class [Ljava.lang.String; Aobject[] Objarray =List.toarray (); at System.out.println (Objarray.getclass ()); -  -Objarray[0] =NewObject ();//cause arraystoreexception -     } -  -      Public Static voidtest3 () in     { -List<string> dataList =NewArraylist<string>(); toDatalist.add ("One"); +Datalist.add ("the"); -  theobject[] Listtoarray =Datalist.toarray (); *  $         //class [Ljava.lang.Object; Returns an Object arrayPanax Notoginseng System.out.println (Listtoarray.getclass ()); -Listtoarray[0] = ""; theLISTTOARRAY[0] = 123; +Listtoarray[0] =NewObject (); A          the}

1, about Test1 ()

Subclass inherits from BaseClass, because each element in the subclass array is a subclass object, so baseclass[] Basearray = subarray; This forced type conversion does not result in an error. This is actually the upward transformation of the Java object, and the subclass array is allowed to be converted into a parent array . However, because the element types in the array are subclass types, basearray[0] = new BaseClass (), error java.lang.ArrayStoreException. This means that if we have 1 object[] arrays, it does not mean that we can store object objects in, depending on the actual type of elements in the array.

2, about Test2 ()

list<string> list = Arrays.aslist ("abc"); It is important to note that the actual type returned is Java.util.arrays$arraylist, not ArrayList. We call

object[] Objarray = List.toarray (); Returns is the string[] array, so we can't put the object in the Objarray array.

3, about Test3 ()

The ToArray () return of the ArrayList object is the object[] array, so we can store any object in the returned object[] array.

As can be seen through test2 and test3, if we have 1 list<string> stringlist objects, when I call object[] Objectarray = Stringlist.toarray (); Objectarray is not necessarily able to place object objects. This is the comment in the source code: C.toarray might (incorrectly) not return object[] (see 6260652). In order to consider this situation, the source has made an if judgment to prevent the wrong array object from causing the exception. Arrays.copyof (elementdata, size, object[].class); This method is used to create 1 object[] arrays so that any object can be stored in the array.

JDK 6260652 Bug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.