Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

array syntax new Pizza[1]

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khalid Mughal, p. 91
What is the meaning of:
new Pizza[1]

<PRE>
class PizzaDriver {
public static void main(String args[]) {
Pizza[][] pizzaGalore = {
{ new Pizza(), null, new Pizza() },
{ null, new Pizza() },
new Pizza[1],
{},
null,
};
}
}
class Pizza {
public Pizza() {
System.out.println("Pizza");
}
}
</PRE>


[This message has been edited by Caroline Bogart (edited November 21, 2001).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new Pizza[1] creates an (anonymous) array of pizzas of length 1.
The array will be able to reference one object of type Pizza ! The created array will hold a null reference as its first component element. To initialize an array created like that you would have to write the following new Pizza[1]{new Pizza()}.
But created like as it is, it is the same as writing new Pizza[1]{null}
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Caroline Bogart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can it be initialized after the pizzaGalore definition?
<PRE>
pizzaGalore[2][0] = new Pizza();</PRE>


[This message has been edited by Caroline Bogart (edited November 21, 2001).]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you can
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Valentin:
" But created like as it is, it is the same as writing new Pizza[1]{null} "
I believe when you create and initialize an array in the same line you do like this:
Pizza [] p= new Pizza[]{null}; (you can't specify the size in this case)
and Pizza [] p= new Pizza[1]{null};//error at compile time ->(Error dumped : misplaced construct(s))
Correct me if I'm wrong.
Best regards

[This message has been edited by Salamina Daniel (edited November 22, 2001).]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic