• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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).]
 
Squanch that. And squanch this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic