• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Array and Polymorphism questions.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am working my way through some SCJP questions and I have come across two that I don't understand.
First one if from ExamLab.

Which is valid:

Ok, I know why A&B are false, but the syntax for C&D&E is completely new to me. C is the only one that is true, but I have no idea why.

- second question removed until the source is cited -
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll move this thread to our SCJP forum, but not before answering your first question.

Eoin Brosnan wrote:Hi,
I am working my way through some SCJP questions and I have come across two that I don't understand.
First one if from ExamLab.

Which is valid:

Ok, I know why A&B are false, but the syntax for C&D&E is completely new to me. C is the only one that is true, but I have no idea why.


A is invalid because you either create an array with a size (new int[0]) or with its values ({}), not both.

B is invalid because it's impossible to only create an array without doing anything with it. I was actually a bit surprised by this, because it is possible to call "new Object();". Apparently the compiler is smart enough to see that an array without a reference is useless, whereas an object can still do things in its constructor.

C uses two "techniques". The first is create an array while immediately assigning its values: new int[][]{{1}}. Then its first element is accessed using [0]; the result is an int[] since the array itself is an int[][].

D tries to do the same, but the dimensions are wrong. new int[]{0} creates an array with one element with value 0. The first [0] returns that element, an int. The second [0] is invalid, since this is only allowed for arrays, not for ints.

E is just wrong.
 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, thanks for the explanation. However isn't option C similar to B in that it creates an array but doesn't assign it to anything?
For example:


Thanks again.
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In example C, you still use the array. You don't assign it to a variable, but you access it nonetheless. And that's the difference with example B, where you create an array and ignore it immediately.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between

int[][] array1 = new int[][] {{1,2,3}, {4,5,6}};
int[][] array2 = {{1,2,3}, {4,5,6}};
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
arun,i think this two array initialisations is same .only two ways of expression.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic