Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Object class MYSTERY

Tags:

java

object

class

While Porting a Game I come to a below statement

Object o = new Object[]{"A","B"};

It's really weird!

But when I try the same with "String" then compiler report me an Error msg

String s = new String[] {"A", "B", "C"}; Error: Type mismatch: cannot convert from String[] to String

Can you please reveal the Mystery of it ?

like image 304
Amit Yadav Avatar asked Dec 20 '22 16:12

Amit Yadav


1 Answers

You have a trivial error in your code. The fact that every class extends Object makes the error more difficult to find.

Since every class (including arrays) extends Object, conversion from A[] to Object is possible.

You wrote int i = new int[] but that's a mistake, you should have written int[] i.

Probably. Object a = new Object[] is not what you wanted to do.

like image 136
usr-local-ΕΨΗΕΛΩΝ Avatar answered Jan 08 '23 00:01

usr-local-ΕΨΗΕΛΩΝ