File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array and Polymorphism questions. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Array and Polymorphism questions." Watch "Array and Polymorphism questions." New topic
Author

Array and Polymorphism questions.

Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
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 -
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
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

Joined: Oct 27, 2005
Posts: 19216

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.
Arun Arun Kumar
Greenhorn

Joined: Sep 03, 2011
Posts: 4
what is the difference between

int[][] array1 = new int[][] {{1,2,3}, {4,5,6}};
int[][] array2 = {{1,2,3}, {4,5,6}};
gihan dissanayaka
Greenhorn

Joined: Sep 03, 2011
Posts: 8
arun,i think this two array initialisations is same .only two ways of expression.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Array and Polymorphism questions.
 
Similar Threads
Khalid Mock Exam
Please hava a look at this question about array !!!
Khalid Mock Exam Array
Stuck on Homework
Clrify results from SCJP mock exam site