8

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 shown below:

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

Then when coming to String why below declaration is wrong:

String s = new String[]{"s","s"};
6
  • 10
    new String[]{"s","s"} is of type String[], not String. Object[] is a subclass of Object, which is why the first one works. Commented Jun 8, 2016 at 7:47
  • String[] s = new String[]{"s","s"}; Commented Jun 8, 2016 at 7:47
  • @AndyTurner Ok, then String[] is not subclass of String. Commented Jun 8, 2016 at 7:50
  • @HarikaChoudaryKanikanti correct. That is generally true of array types (except Object[] and Object); but also String is a final class, so it has no subclasses. Commented Jun 8, 2016 at 7:54
  • 1
    Since nobody's pointed it out, this version is cleaner and equally valid: String[] s = {"s","s"}; Commented Jun 9, 2016 at 6:37

6 Answers 6

24

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];
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

1

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

Comments

1

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...

Comments

0
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"};

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.