Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Fri Feb 19, 2016 10:55 am
by NickWoodward
Option 3:

new Object[]{ "aaa", new Object(), new ArrayList(), new String[]{""} };

doesn't compile for me, it points to all the elements in the array stating 'incompatible types' (other than new Object()). which seems odd, because you're right, it should be allowed!


also, would this have to be assigned to a 2d array reference?

thanks,

Nick

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Fri Feb 19, 2016 11:49 am
by admin
Please paste the full exact code that you are trying to compile.
Why do you think it should be assigned to a 2d reference?

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Fri Feb 19, 2016 12:04 pm
by NickWoodward
sorry, it was just my text editor on the blink, was compiling an earlier verion even though the new one was saved. fixed now!

anyway, if the Object array has a reference to a string array, how is that different to a jagged 2d array?

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Fri Feb 19, 2016 7:38 pm
by admin
In a 2d array every element must be a 1d array. In a 1d array, that is not the case. You can't assign an element of a 1d array to another 1d array reference. But you can do so with the elements of a 2d array.

In a 1d array of Objects, it is posible to store any array only because every array IS-A Object. You will still need a cast to assign that element to an array reference. Had it been a 2d array, no cast would have been needed.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Sat Feb 20, 2016 5:44 am
by NickWoodward
ah ok, thanks for that :D

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Nov 21, 2016 12:53 am
by mj.anjuthan
How does this '{ }' work?

Does this return a reference to an empty array object?
If yes, why the reference cannot be stored in in an Object array?
If no, what does it return?

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Nov 21, 2016 2:00 am
by admin
The meaning of {} depends on the context. For example, it can mean a code block or a class declaration or an array declaration. The compiler figures out what it means based on that context and what it contains. If you just type an empty the compiler is not able to decide.


HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Nov 21, 2016 2:07 am
by mj.anjuthan
I meant in using "{}" it in the array declarations like given in the options of this question.

new Object[]{ "aaa", new Object(), new ArrayList(), {} };
In this "{}" it gives an error.

Object arr[][] = new Object[][] {new String[5], {} }
But here it gives an error.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Jan 30, 2017 9:34 am
by PeterD
Same question:
I meant in using "{}" it in the array declarations like given in the options of this question.

new Object[]{ "aaa", new Object(), new ArrayList(), {} };
In this "{}" it gives an error.

Object arr[][] = new Object[][] {new String[5], {} }
But here it gives no error according to the explanation.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Sun Feb 12, 2017 1:32 pm
by ElizabethCM
Hi Admin,

I just want to make sure I understand this:
So this works because everything is an Object in Java, including an array of Strings:
new Object[]{ "aaa", new Object(), new ArrayList(), new String[]{""} };

Had it been anything different than Object, we would need a 2 dimensional array, right?
String[] str1 = new String[]{ "aaa", new String(""), new String(""), new String[]{" "} };
this does not work.
but this works:
String[][] str1 = new String[][]{new String[]{" "} };

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Sun Feb 12, 2017 10:01 pm
by admin
Had it been anything different than Object, we would need a 2 dimensional array, right?
String[] str1 = new String[]{ "aaa", new String(""), new String(""), new String[]{" "} };
this does not work.
It does not work because each element of str1 has to be a String and new String[]{" "} is not a String.
but this works:
String[][] str1 = new String[][]{new String[]{" "} };
It works because str1 is defined such that each element of str1 is String array. new String[]{" "} is a valid element of str1.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Thu Oct 05, 2017 7:14 am
by JuergGogo

Code: Select all

public class TestClass {
    
    public static void main(String[] args)  {
        
        String[] sa = { };
        if ( sa == null ) System.out.println( "null" );     // --> false, no output
        if ( sa.length == 0 ) System.out.println( "0" );   // --> true, output: 0
        
        Object arr[][] = new Object[][] { new String[5], {} };
        System.out.println( arr[0].getClass() );          // --> class [Ljava.lang.String;
        System.out.println( arr[1].getClass() );          // --> class [Ljava.lang.Object;
        System.out.println( arr[1].length );               // --> output: 0           
        
    }
}
This question is quite tricky for me, and my intuition told me not to bother Paul again for this one. 8-)

So, the first example with the array of Strings is an identical situation like starting a class from command-line without any arguments ( public static void main(String[] args ). In this case args/sa are not null, there is an array with length 0.

The second example proves again that empty {} creates an array with length 0. In this example an array of Object, not Strings.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Mar 18, 2019 7:01 am
by zel_bl
If I understand correctly
in Object[] arr = new Object[]{ "a", {} } {} is an empty array of Objects (Object[]) and can not be converted to Object.
Yet arrays are objects...
Could you clarify the {} in arr. Why Object[] can not be an Object.
Thanks

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Mar 18, 2019 7:19 am
by admin
Everything in Java is an object. So, an empty array of objects can certainly be "converted" to Object. That is not the reason why Object[] arr = new Object[]{ "a", {} } fails compilation. The compiler complains because {} gives no indication of what object you are trying to create and the compiler assumes that you are trying to create an object of Object class (because arr is defined as Object[] and therefore each element of this array is Object). But {} is not a valid way to create an object of Object class.

Indeed, Object[] arr = new Object[]{ "a", new Object[]{} } ; compiles fine because new Object[]{} is a valid way to create an empty array of type Object and that array is an Object.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Mar 18, 2019 7:57 am
by zel_bl
If you could explain why is {} in
Object[][]ar = new Object[][] {
{ new String[]{},"a",new Object[] {} },
{}
};
valid way to create an object?

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Mar 18, 2019 8:04 am
by admin
It is not. But it is a valid way to create an Object[]!

For more and precise details, you will need to go through the relevant sections of the JLS. Ultimately, the JLS decides the rules of what is valid and what is not.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Mar 18, 2019 8:07 am
by zel_bl
Clear and prompt explanation.
Thank you

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Oct 07, 2019 9:24 am
by elecdream
I wonder why answer no. 3 is not valid?

Object obj = new Object[]{ "aaa", new Object(), new ArrayList(), new String[]{""} };
gives not compiler error.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Mon Oct 07, 2019 10:20 am
by admin
Option 3 is indeed shown as a correct option along with option 1. Options 2 and 4 are incorrect.

Re: About Question enthuware.ocajp.i.v7.2.1207 :

Posted: Thu Dec 21, 2023 9:06 pm
by yulinxp
To summarize,

Code: Select all

Every element must be Object.
new Object[]{ "aaa", new Object(), new ArrayList(), {} };

Code: Select all

//invalid
 Object b = {};   
new Object[]{ {} };
But below is valid.

Code: Select all

Object[] b1 = {};
new Object[][]{ {} };