If an array of Object is an Object, why is an array of String not a String

后端 未结 6 1389
心在旅途
心在旅途 2021-02-15 12:06

Object class is super class to every class in Java. So every class should inherent the properties or behavior of Object class.

Then we can declare array of objects as sh

相关标签:
6条回答
  • 2021-02-15 12:40

    array is an collection of objects or collection of primitive datatypes while string is a sequence of characters. as you know object is super class of every other class that's why you an do like below: Object c = new Object[] {1,2,"22" };

    String class is not super class of array type...so you can't perform like below..

    String s = new String[]{"s","s"};
    

    hope this will help you...

    0 讨论(0)
  • 2021-02-15 12:45
    Object c = new Object[] {1,2,"22" };
    

    is valid due to the fact that an array of Objects is still an Object. You could specify that c is an array of Objects at declaration level too, like this:

    Object[] c = new Object[] {1,2,"22" };
    

    However,

    String s = new String[]{"s","s"};
    

    is not valid, since an array of Strings is not a String. You need either to convert your array into a String, like this:

    String s = String.join(",", new String[]{"s","s"});
    

    or to store it as an array, like this:

    String[] s = new String[]{"s","s"};
    
    0 讨论(0)
  • 2021-02-15 12:50

    First, array holds the reference of values which are in heap.

    Second, Object class is mother class of every other class in java. so object array can hold any kind of reference value but string array will only hold reference of string data type and each and every reference is referring to separate string (string[0]= "a",string[1]="stackOverFlow"...).

    Third, A string is a sequence of character in java.

    so, a sting array can not be a string because it is not referring to sequence of characters but referring to string type of objects residing in heap.

    0 讨论(0)
  • 2021-02-15 12:51

    new String[]{"s","s"} is of type String[], not String. T[] is not a subclass of T (unless T happens to be Object).

    Object[] is a subtype of Object, which is why the first one works. In fact, all array types are subtype of Object, including, perhaps surprisingly, arrays of primitives, like int[], even though int is not an Object (*).

    You could write the first one using more specific types:

    Object[] c = new Object[] {1,2,"22" };
    

    You can write the second as any of the following:

    String[] s1 = new String[]{"s","s"};
    Object[] s2 = new String[]{"s","s"};
    Object s3 = new String[]{"s","s"};
    

    Incidentally, s2 demonstrates that arrays in Java are covariant. This is problematic, since you can legally write:

    s2[0] = new Object();
    

    which would fail at runtime with an ArrayStoreException, since you can't store Object references in a String[].

    This is one of the reasons why authors like Josh Bloch give the advice "Prefer lists to arrays" (see Effective Java 2nd Ed Item 25), since Java collections like List are not covariant, and so don't suffer the same issue.


    (*) Just to add to the confusion, primitive arrays are not subtypes of Object[], as primitives are not subtypes of Object. For example, it would be a compile-time error to write:

    Object[] illegal = new int[5];
    
    0 讨论(0)
  • 2021-02-15 12:55

    Somewhat confusingly, an Object[] is an Object. (For one thing this is how Java can implement zero-length arrays, and allow arrays to be function return values).

    So assigning an Object[] instance to a reference of type Object type makes sense.

    But assigning a String[] instance to a reference of type String does not make sense. (But note that String[] is also an Object.)

    So

    1. Object c = new Object[] {1,2,"22" }; Makes sense
    2. String s = new String[]{"s","s"}; Doesn't make sense
    3. Object s = new String[]{"s","s"}; Makes sense
    0 讨论(0)
  • 2021-02-15 13:04

    Its basic Java principle, Everything is an Object, thus you can use Object reference for everything like Object o = new AnyOtherClass()

    You can use reference of a class for its sub classes like List l = new Arraylist()

    But String[] is an Array and Array is not an ancestor of String

    0 讨论(0)
提交回复
热议问题