Why does this code compile?
Object[] object = new String[5][5];
I mean, why can I do that if I'm creating an array object with different dimensions than specified in the reference variable?
This doesn't compile:
String[] strings = new String[5][5];
So what is happening here?
By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.
String objects have the feature of adding a property to an object. In general, the string(with a small 's') denotes a primitive whereas String(with an uppercase 'S') denotes an object. JavaScript supports five types of primitives and string is one of them.
The first one compiles because String[]
is an Object
. The 2nd one doesn't compiles because String
is not String[]
.
Object[] object = new String[5][5]; // Means each element is an String[] which is an Object as well.
String[] strings = new String[5][5]; // Also Means each element is an String[] which is not same as just String.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With