Why cant I cast Object[] to StringBuilder[] in Java?

Laz London
ArrayList<StringBuilder> al = new ArrayList<>(
                              Arrays.asList(new StringBuilder[]{
                                  new StringBuilder("Oracle"), 
                                  new StringBuilder("Java"), 
                                  new StringBuilder("Sun"), 
                                  new StringBuilder("DataBase")}));

StringBuilder[] al2array = (StringBuilder[]) al.toArray();

If al.toArray() returns an Object[] which i know that its actually a StringBuilder[], then why cannot i cast it?

Hot Licks

You most certainly can cast a variable typed as Object[] to one typed as StringBuilder[]. The compiler will not complain, and it will execute without error, IF the source reference does indeed reference a StringBuilder[].

public class ArrayCastTest {
    public static void main(String[] argv) {
        Object[] objArray;
        StringBuilder[] sbArray;
        objArray = getArray();
        sbArray = (StringBuilder[]) objArray;
        System.out.println(sbArray.toString());
    }

    public static Object[] getArray() {
        return new StringBuilder[5];
    }
}

This executes without error:

C:\JavaTools>javac ArrayCastTest.java

C:\JavaTools>java ArrayCastTest
[Ljava.lang.StringBuilder;@76f4da6d

C:\JavaTools>

The important thing to understand is that cast is "overloaded" -- it transforms simple values (like int to char) but it does not transform object references -- it only changes the declared type of the reference. The problem is that ArrayList.toArray() returns an Object[]. To get your Object[] into a StringBuilder[] use System.arraycopy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why cant i cast an Action<Int32> to Action<Object>?

From Dev

confused ,why Object[] objs (type of objs[i] is int[]) cant cast into int[][]

From Dev

Why can I not cast a double object to a byte?

From Dev

Why I cannot cast an object to an interface it is implementing?

From Dev

Why java creates error to cast a object as callable?

From Dev

StringBuilder constructor accepts a StringBuilder object - why?

From Dev

StringBuilder constructor accepts a StringBuilder object - why?

From Dev

Why cant I add an array as a member to my object

From Dev

Why Cant I Pass This JSON Object to Another Activity

From Dev

Why cant I center this?

From Dev

Why must I cast in Kotlin but not Java?

From Dev

java cast ArrayList<StringBuilder> to ArrayList<String>

From Dev

Why cant I use the Collections.max() function for this code? - java

From Dev

Why cant I use the Collections.max() function for this code? - java

From Dev

Why cant I change the the file extension of a desired file in java?

From Dev

Why can't I cast an object to a constrained generic?

From Dev

Why can't I cast EntityCollection<TEntity> to ICollection<Object>?

From Dev

Why can I cast an object? Can it be done with other objects?

From Dev

Why can I not cast an object with a value of 24 to a double?

From Dev

Why do I get the error of unable to cast object

From Dev

Why cannot I cast com object in c#?

From Dev

Why cant I calculate the mean?

From Dev

Why cant i change a instance?

From Dev

Why does a key in a Map must be an object and not a primitive and Why cant i use primitive type as key in a map

From Dev

Java cast Object to MyClass

From Dev

Java cast Object to MyClass

From Dev

How do I cast a Java Object into a List of integers?

From Dev

Why can not use a String to initialize a StringBuilder object?

From Dev

Why do I need to cast HttpURLConnection in this Java example?

Related Related

  1. 1

    Why cant i cast an Action<Int32> to Action<Object>?

  2. 2

    confused ,why Object[] objs (type of objs[i] is int[]) cant cast into int[][]

  3. 3

    Why can I not cast a double object to a byte?

  4. 4

    Why I cannot cast an object to an interface it is implementing?

  5. 5

    Why java creates error to cast a object as callable?

  6. 6

    StringBuilder constructor accepts a StringBuilder object - why?

  7. 7

    StringBuilder constructor accepts a StringBuilder object - why?

  8. 8

    Why cant I add an array as a member to my object

  9. 9

    Why Cant I Pass This JSON Object to Another Activity

  10. 10

    Why cant I center this?

  11. 11

    Why must I cast in Kotlin but not Java?

  12. 12

    java cast ArrayList<StringBuilder> to ArrayList<String>

  13. 13

    Why cant I use the Collections.max() function for this code? - java

  14. 14

    Why cant I use the Collections.max() function for this code? - java

  15. 15

    Why cant I change the the file extension of a desired file in java?

  16. 16

    Why can't I cast an object to a constrained generic?

  17. 17

    Why can't I cast EntityCollection<TEntity> to ICollection<Object>?

  18. 18

    Why can I cast an object? Can it be done with other objects?

  19. 19

    Why can I not cast an object with a value of 24 to a double?

  20. 20

    Why do I get the error of unable to cast object

  21. 21

    Why cannot I cast com object in c#?

  22. 22

    Why cant I calculate the mean?

  23. 23

    Why cant i change a instance?

  24. 24

    Why does a key in a Map must be an object and not a primitive and Why cant i use primitive type as key in a map

  25. 25

    Java cast Object to MyClass

  26. 26

    Java cast Object to MyClass

  27. 27

    How do I cast a Java Object into a List of integers?

  28. 28

    Why can not use a String to initialize a StringBuilder object?

  29. 29

    Why do I need to cast HttpURLConnection in this Java example?

HotTag

Archive