Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Syntax Confusion

 
Mustakimur Rahman
Greenhorn
Posts: 13
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I got it now.
Thanks all.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic