• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Assignments

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exercise 7, Chapter 3, K&B book

I don't understand why this compiles:

1. class Zippy {
2. String[] x;
3. int[] a [] = {{1,2}, {1}};
4. Object c = new long[4];
5. Object[] d = x;
6. }

Can anyone explain lines 4&5 please? What does d refer to?

Thanks
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

Arrays are always objects, whatever you store in the array (even primitive). That's why lines 4 and 5 compiles.

Hope this helps.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Galina,


String[] x;
Object[] d = x; // d =null

Can anyone explain lines 4&5 please? What does d refer to?



d refers to a 1- D String array .Remember since d is a 1 - D it can only refer to a 1-d array or anything that can be implictly cast like


dog d = new dog();
Object[] obj ;
obj = {new dog(),d}; // Since dog IS - A Object.Arrays make use of polymorphism.

Hope that helped.For a more thorough understanding check K & B book.

Uttara Rishi.
[ March 11, 2008: Message edited by: Uttara Rishi ]
 
Galina Zapletnuka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, guys.
I'm primarily concerned about assigning an uninitialized variable (x) to another varaible (d) and what happens as a result.
I think I got it now
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic