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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post by NickWoodward »

ah ok, thanks for that :D

mj.anjuthan
Posts: 10
Joined: Thu Nov 10, 2016 3:07 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

mj.anjuthan
Posts: 10
Joined: Thu Nov 10, 2016 3:07 am
Contact:

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

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

PeterD
Posts: 4
Joined: Thu Jan 26, 2017 8:45 am
Contact:

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

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

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post 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

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

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

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post 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

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post by zel_bl »

Clear and prompt explanation.
Thank you

elecdream
Posts: 1
Joined: Mon Oct 07, 2019 9:19 am
Contact:

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

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

admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Option 3 is indeed shown as a correct option along with option 1. Options 2 and 4 are incorrect.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests