• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array definition in one step

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've got a question about array definition as described in Mala Gupta "OCA Java SE 7 cert guide 2013 ed." at page 203.

Here the author describes two different ways to define an array in one step: by using the keyword new or not. Actually I cannot understand if there's a difference between the two methods.

I try to explain better: if I want to define an array in three steps (declaration, allocation and initialisation) I necessarily must use "new" operator while allocating the array, otherwise it doesn't compile.

At first I thought the difference was in the kind of values I want to store in the array (actual values or addresses), but it cannot be, otherwise I should have the possibility to do it by defining the array in three steps too.

Does anyone know if there's a difference between using "new" operator or not, and what actually consists of?

I hope my question is clear enough, I am not english mother tongue.

Thank you in advance.

Ps.:
array definition with keyword "new":

array definition without keyword "new":

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

As you noticed, in this example, both work. That's because the line is a variable declaration. Try it again when calling a method.

For example,



Which format can you pass in? Hint: Only one will work.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pagano monello wrote:
Here the author describes two different ways to define an array in one step: by using the keyword new or not. Actually I cannot understand if there's a difference between the two methods.

One without new is called array initializer and the one with new is called array creation expression. Both do the same thing but array initializer can only be used at field or local variable declaration, or as part of an array creation expression (e.g. when you creating a multidimensional array).

This is just how the language designers designed it. I don't see any special reason for providing the initializer way other than reducing a few keystrokes. Since the compiler can figure out the type of array you are creating, it lets you avoid typing the new keyword and the type name.
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do thank you both, I take note of your answers.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pagano,

First of all, a warm welcome to CodeRanch!

pagano monello wrote:Does anyone know if there's a difference between using "new" operator or not, and what actually consists of?

Besides the 2 great replies, I like to add a few other points about array creation and initialization. Fasten your seatbelt, here we go!

To create an array with the new operator:
When you created the array each element is initialized with a default value. For int it's 0 as shown in the following code snippet:Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Once you created the array you can assign values to each element of the array:Or you could use a for loop to simplify this process:Output: [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]

If you know the number of elements and all values, you can use the shortcut syntax to create and initialize an array:

Let's assume we have a method which takes an int array as parameter. For example:So you can easily pass an int array to the print method:
But imagine you want to pass an int array to the print method without having an array variable. Can you simply use the shortcut syntax? Let's try:No, you can't! The above statement generates a compiler error. But you can create an (anonymous) array using the new operator as shown here:Output: [1, 2, 3]
So this syntax is mostly used when you need to invoke a method which takes an array parameter and you don't want to create an array variable (for whatever reason).

If you know the array size and its elements, you'll always use the shortcut syntax because it's concise and easy to read. For example creating the identity matrix for 2x2 and 3x3:
But what if you needed to create a method which created the identity matrix for a given n? You don't know the array size, so you can't use the shortcut syntax, you first have to create the array and then initialize it appropriately. Let's create this method:Now you can use this method to create the identity matrix for 2x2 and 3x3 matrices, even for a 20x20 matrix And with an overloaded version of the print method you can verify the result of the identityMatrix method:Output: [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]] Yay, it works!

Hope it helps!
Kind regards,
Roel
 
Craig Watkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But what if you needed to create a method which created the identity matrix for a given n? You don't know the array size, so you can't use the shortcut syntax, you first have to create the array and then initialize it appropriately. Let's create this method:

Hi Roel,

I found this thread while trying to look up anonymous arrays (currently working with OCA Study Guide - Boyarsky & Selikoff / Sybex 2015).

But I noticed in your code you have a for loop with condition (i><n)

Am I right in saying this "x><y" is illegal in Java (I have tried researching it elsewhere, compiling it myself (it doesn't) etc., and while I first thought it was a typo, I see you have it in two places i><n (line3) and j><n (line4) so just wanted to double check I wasn't missing something?

I don't know if I'm allowed to add my original question here, but I'm confused on Anonymous Arrays:
A few google searches seem to agree that an anonymous array is an array without a reference variable (which fits with how you've described it here)
However in B&S(Sybex 2015 p.120), an anonymous array is one where "... you don't specify the type and size."

B&S's description is very confusing for me because a few paragraphs up on the same page where they show how to declare an array and specify initial values on the same line, they mention how this approach specifies the size and type of the array:


Also, going on the "[an anonymous array is one where] ... you don't specify the type and size" ... am I correct in saying that when you declare an array, you always need to specify the type? (I feel silly asking as I feel so sure one would always need to specify the type, but after reading S&B's description of an anonymous array not requiring type and size be specified ....)

P.S. I have checked the errata for the book.


>
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Watkins wrote:
But I noticed in your code you have a for loop with condition (i><n)

Am I right in saying this "x><y" is illegal in Java (I have tried researching it elsewhere, compiling it myself (it doesn't) etc., and while I first thought it was a typo, I see you have it in two places i><n (line3) and j><n (line4) so just wanted to double check I wasn't missing something?


P.S. I have checked the errata for the book.


I don't know if this has been fixed yet ... but ... it is *not* a typo, nor should it be mentioned in the errata for the book.

Anyway, there was a bug with the ranch software a ways back (not sure if it has been fixed yet), but one of the side affects of the bug was that code got corrupted with "><" conditionals. Sorry for the inconvenience.

Henry
 
Craig Watkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thank you for the quick reply. Just to re-iterate, there were two parts:

1. Whether "x><y" is legal - which you've cleared up for me, thank you    // this has nothing to do with the S&B(Sybex 2015) book. nor the errata thereof

2. S&B's description of anonymous arrays, where they say (on page 120):

... is an anonymous array because you don't specify the type and size

It is this second point I'm confused about as in the declaration we have specified type (int) and size (3) ??  as well as the inconsistency between this definition and google search definitions, and Roel's description and example of an anonymous array (and also this second point which may need to be referenced on the book's errata page)

Thank you again >
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic