1

Try this code at Ideone.com:

Object o1 = new int[3]; 
Object[] o2 = new int[3]; //C.E

Why second statements gives compile time error while first one does not ?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 4
    Arrays are objects (first line). Arrays of `Object` are not the same as arrays of primitives (second line). – markspace Apr 01 '23 at 21:07
  • 1
    error as specified in [JLS 15.26.1. Simple Assignment Operator =](https://docs.oracle.com/javase/specs/jls/se20/html/jls-15.html#jls-15.26.1): "If the type of the right-hand operand is not assignment compatible with the type of the variable (§[§5.2](https://docs.oracle.com/javase/specs/jls/se20/html/jls-5.html#jls-5.2)), then a compile-time error occurs." – user16320675 Apr 01 '23 at 21:58
  • Like @markspace says: Array inherits from Object, although I did not know that before today. Check out this article: https://www.geeksforgeeks.org/array-primitive-type-object-java/# – koZmiZm Apr 02 '23 at 19:20
  • The array types `int[]` and `Object[]` are both subtypes of `Object`, but none of them is a subtype of the other. Also, why would you want to make that assignment? Surely there’s a way to get to your end goal, just a different way and possibly slightly wordier. – Ole V.V. Apr 03 '23 at 08:20
  • Similar: [Cast primitive type array into object array in java](https://stackoverflow.com/questions/5606338/cast-primitive-type-array-into-object-array-in-java) – Ole V.V. Apr 03 '23 at 08:22
  • This could be helpful too? [How can I convert int\[\] to Integer\[\] in Java?](https://stackoverflow.com/questions/880581/how-can-i-convert-int-to-integer-in-java) `Integer[]` is a subtype of `Object[]` and therefore assignable to `Object[]` (which isn’t completely logical, but it works). – Ole V.V. Apr 03 '23 at 08:31
  • Thank u all for your replies, now it is very clear especially : "The array types int[] and Object[] are both subtypes of Object" @OleV.V. – Elias El hachem Apr 03 '23 at 13:01

1 Answers1

1

tl;dr

  • In Java, every array is itself an object. (your first line)
  • In Java, an array of primitive values cannot be assigned to a variable declared for an array of object references. Primitives are not compatible with objects. (your second line)

Details

As the Comments explain:

  • Your first line creates an array, and puts a reference to that array in a variable of type Object. This works because every array (of any type) is itself a subtype of the Object class. See the Java specifications.
  • Your second line creates an array specifically to hold int values. The int type is a primitive type, not an object-oriented type. You cannot assign a reference pointing to an array of primitives to a variable declared to hold an array of Object objects. That is a type mismatch, as primitives and objects are not directly compatible.

Understand that Java has two type systems, operating in parallel:

  • Object-oriented types. Modern, sophisticated, powerful, flexible.
  • Primitive types. Originally included to make C code easier to port to Java — a crucial feature to meet market expectations at that time. Also useful when high-performance and/or memory-efficiency is paramount.

Work is underway to blur the distinction between these two type systems in future versions of Java. But that work is far from complete. So in the meantime you as a Java programmer must be fully cognizant of each realm. Auto-boxing helps to bridge the divide between those realms, but the divide is still very much present.

Let's make that second line work. Change the int to Integer. The Integer class is a wrapper class. Each Integer object holds an int value. Now on the right side we instantiate an array of Integer objects, where each element of that array is null.

Object[] o2 = new Integer[3] ; 

Arrays.toString( o2 ): [null, null, null]

We can assign something to each element.

  • We can assign an Integer object of course.
  • We can assign an int primitive. The auto-boxing feature in Java automatically wraps that int value inside of an Integer object, then places a reference to that object within our array.
o2[0] = new Integer( 7 ) ;  // Assign an `Integer` object holding a `int` primitive within.
o2[2] = 42 ;                // Assign a primitive value where auto-boxing automatically wraps the `int` value in an `Integer` object, then places a reference within the array.

See this code run at Ideone.com.

Arrays.toString( o2 ): [7, null, 42]

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154