I am trying to understand the underlying difference between initializing Strings and StringBuffer
The following code works fine. (al is an ArrayList<String>)
String[] sa = new String[al.size()];
System.arraycopy(al.toArray(), 0, sa, 0, al.size());
However when i use StringBuffer array it wont work.
StringBuffer[] sa = new StringBuffer[al.size()];
System.arraycopy(al.toArray(), 0, sa, 0, al.size());
It gives me following exception
java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at practice.ArrayListDemo.main(ArrayListDemo.java:34)
Can someone help me understand the logic/reason? It is not clear how string being immutable makes a difference.