If the Object array is an object, then why is the String array not a string

An object class is a superclass for each class in Java. Therefore, each class must have properties or behavior of the Object class.

Then we can declare an array of objects, as shown below:

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

Then, when you get into String, why the below declaration is incorrect:

 String s = new String[]{"s","s"}; 
+8
java
source share
6 answers

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

Object[] is a subtype of Object , so the first one works. In fact, all types of arrays are a subtype of Object , including, perhaps surprisingly, arrays of primitives, for example int[] , although int not Object (*).

You can write the first 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"}; 

By the way, s2 demonstrates that arrays in Java are covariant. This is problematic as you can legally write:

 s2[0] = new Object(); 

which will fail at runtime with an ArrayStoreException , since you cannot store Object references in String[] .

This is one reason why authors such as Josh Bloch give advice on “Preferred Lists for Arrays” (see Effective Java 2nd Ed Item 25), because Java collections such as List are not covariant and therefore do not suffer the same. problem.


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

 Object[] illegal = new int[5]; 
+22
source share

Somewhat vaguely, Object[] is an Object . (First, this is how Java can implement zero-length arrays and allow arrays to return function values).

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

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

So,

  • Object c = new Object[] {1,2,"22" }; Has the meaning
  • String s = new String[]{"s","s"}; Does not make sense
  • Object s = new String[]{"s","s"}; Has the meaning
+3
source share

Its basic Java principle: all Object , so you can use the Object link for everything, like Object o = new AnyOtherClass()

You can use the class reference for your subclasses, for example List l = new Arraylist()

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

+1
source share

array is a collection of objects or a collection of primitive data types, and a string is a sequence of characters. since you know that an object is a superclass of every other class, why are you doing as below: Object c = new Object[] {1,2,"22" };

The String class is not a superclass of an array type ... so you cannot execute as shown below.

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

hope this helps you ...

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

true because the Object array is still an Object . You can specify that c is an array of Object at the declaration level, for example:

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

but

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

invalid because the String array is not a String . You need to either convert the array to String , for example:

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

or save it as an array, for example:

 String[] s = new String[]{"s","s"}; 
0
source share

First, the array contains a reference to the values ​​in the heap.

Secondly, the Object class is the mother class of any other class in java. therefore, an array of objects can contain any reference value, but a string array will contain only a reference to the data type of the string, and each link refers to a separate string (string[0]= "a",string[1]="stackOverFlow"...) .

Thirdly, a string is a sequence of characters in java.

therefore, the sting array cannot be a string, because it does not belong to a sequence of characters, but refers to the string type of objects on the heap.

0
source share

All Articles