aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Array Syntax Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Array Syntax Confusion" Watch "Array Syntax Confusion" New topic
Author

Array Syntax Confusion

Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

Few days ago, I have faced a syntax like that:

I still does not find the meaning of it. I tries this:

It shows:

Can anyone help me out to find the answer?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4491
    
    8

In Java, you can't change the size of an array once created. So you have to provide its size when you first create it. There are two ways of doing this. One is just to give the size:
The other is to actually provide the initial values for the array:


So that means
is creating an array containing no values. So it has size 0 - it's equivalent to int[] eArr3 = new int[0];

Which means that when you try to access eArr3[0], you're accessing the first element of an array with no elements. So it's out of bounds.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5608
    
  15

Matthew Brown wrote:So it has size 0 - it's equivalent to int[] eArr3 = new int[0];

Instead of taking Matthew on his word, you can see it for yourself. Simply add this statement System.out.println(eArr3.length); just after the line where you created array eArr3 and see what happens.
Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

Hmm, I got it now.
Thanks all.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Also remember that creating an array without explicitly initializing values or specifying its size between the brackets to the right of the equals sign will cause a compilation error. This is will happen whether or not you try to access an individual element of the array.

So either do this:

I hope this helps someone.

Regards,

T3d
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5608
    
  15

And based on the given examples a little bit of code advice Place brackets with the declaration type, not the name
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Roel De Nijs wrote:And based on the given examples a little bit of code advice Place brackets with the declaration type, not the name


Roel,

This is an interesting article from Oracle. Thank-you for posting. The OCAJ7 exam will have brackets in both spots (for extra confusion and difficulty), but I think this article is correct that placing brackets by the type is more readable.

Respectfully,

Ted
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5608
    
  15

Ted North wrote:he OCAJ7 exam will have brackets in both spots (for extra confusion and difficulty)

That's definitely true. It will be a nice mix on the exam: int[][] a1; String [] a2[]; double a3[][]; byte [] a4 [] []; and so on...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Syntax Confusion