• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

not initialized

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At page 269 of Sierra&Bates' book SCJP for Java 5, there is a question:

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

The answer given as correct is "COmpilation succeeds" because all the array declarations are legal...
...but at line 5, x is not initialized, so the correct answer should be "Compilation fails due only to an error on line 5."

I try it with a compiler and it did not compile...

Bye,
Alberto
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables are implicitly initialized (to zero, false or null), only local variables must be initialized explicitly.
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alberto,
Instance variables are initilized automatically.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instance or static variables will be automaticaly initialized,in your example string array will be initialized with null which it is assiging to
object array,so no error will occur.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be no error at compile time since the class instance object variables gets intialized to null. Take a look at the following code modified.
class Zippy {
String[] x;
int[] a [] = {{1, 2}, {1}};
Object c = new long[4];
Object[] d = x;
{
System.out.println(d);
}

public static void main(String arg[]){new Zippy();}
}

It gives you output==>null
 
Alberto Cancello
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops...
I put all inside a main() method.

Thank you.

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

Pls. help me understand the following..I've customized the code a bit:



Line 6:
two dimension int array a assigned to an Object d2 --> code compiles

Line 7:
two dimension int array a assigned to a single dimension Object array d3 --> code compiles

Line 8:
two dimension int array a assigned to double dimension Object array d4 --> compiler error!

Q. Why does the code compiles with respect to line 6 and line 7 and NOT for line 8? What are the differences?

--------------
Ravinder
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ravinder Singh:

Line 6:
two dimension int array a assigned to an Object d2 --> code compiles

Line 7:
two dimension int array a assigned to a single dimension Object array d3 --> code compiles

Line 8:
two dimension int array a assigned to double dimension Object array d4 --> compiler error!

Q. Why does the code compiles with respect to line 6 and line 7 and NOT for line 8? What are the differences?



The variable "a" is an array of array of ints.

Remember that an array is an object -- it inherits from object -- so...

- The variable "a" is also an object.

And this can also be further applied as...

- an array of ints is also an object.

hence...

- The variable "a" is also an array of objects.

And finally...

- The variable "a" is not an array of array of objects. It is an array of arrays of ints.

Henry
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool concept..
Thanks Henry!

-------------
Ravinder
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic