Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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"};
like image 408
Harika Choudary Kanikanti Avatar asked Jun 08 '16 07:06

Harika Choudary Kanikanti


People also ask

Is array an object or a class?

Hence we can say that array is also an object. Now the question also arises, every time we create an object for a class then what is the class of array? In Java, there is a class for every array type, so there’s a class for int [] and similarly for float, double etc.

How to check if an object is an array or not?

The getClass () method acts as the intermediate method which returns an runtime class of the object, which enables the terminal method, isArray () to verify it. Let us see a program to check if an object is an array or not − Checking for str... The Object is not an Array Checking for atr...

What is the difference between array and element?

A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 2^32.

What is every array type in Java?

Every array type implements the interfaces Cloneable and java.io.Serializable. In the Java programming language, arrays are objects (§ 4.3.1 ), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.


4 Answers

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];
like image 91
Andy Turner Avatar answered Jan 19 '23 00:01

Andy Turner


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
like image 44
Bathsheba Avatar answered Jan 19 '23 00:01

Bathsheba


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

like image 40
Abdullah Ahçı Avatar answered Jan 19 '23 01:01

Abdullah Ahçı


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

like image 24
Sanidhya Kumar Avatar answered Jan 19 '23 00:01

Sanidhya Kumar