• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 22780
131
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: 22780
131
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic