About Question enthuware.ocajp.i.v7.2.1207 :
Moderator: admin
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1207 :
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
Please paste the full exact code that you are trying to compile.
Why do you think it should be assigned to a 2d reference?
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.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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?
anyway, if the Object array has a reference to a string array, how is that different to a jagged 2d array?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
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.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
ah ok, thanks for that
-
- Posts: 10
- Joined: Thu Nov 10, 2016 3:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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?
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Thu Nov 10, 2016 3:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
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.
-
- Posts: 4
- Joined: Thu Jan 26, 2017 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
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.
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
It does not work because each element of str1 has to be a String and new String[]{" "} is not a 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.
It works because str1 is defined such that each element of str1 is String array. new String[]{" "} is a valid element of str1.but this works:
String[][] str1 = new String[][]{new String[]{" "} };
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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
}
}
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.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
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.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
If you could explain why is {} in
Object[][]ar = new Object[][] {
{ new String[]{},"a",new Object[] {} },
{}
};
valid way to create an object?
Object[][]ar = new Object[][] {
{ new String[]{},"a",new Object[] {} },
{}
};
valid way to create an object?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
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.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
Clear and prompt explanation.
Thank you
Thank you
-
- Posts: 1
- Joined: Mon Oct 07, 2019 9:19 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
I wonder why answer no. 3 is not valid?
Object obj = new Object[]{ "aaa", new Object(), new ArrayList(), new String[]{""} };
gives not compiler error.
Object obj = new Object[]{ "aaa", new Object(), new ArrayList(), new String[]{""} };
gives not compiler error.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
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.
-
- Posts: 12
- Joined: Sat Dec 02, 2023 12:54 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1207 :
To summarize,
But below is valid.
Code: Select all
Every element must be Object.
new Object[]{ "aaa", new Object(), new ArrayList(), {} };
Code: Select all
//invalid
Object b = {};
new Object[]{ {} };
Code: Select all
Object[] b1 = {};
new Object[][]{ {} };
Who is online
Users browsing this forum: No registered users and 29 guests