aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes not initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "not initialized" Watch "not initialized" New topic
Author

not initialized

Alberto Cancello
Greenhorn

Joined: Apr 27, 2006
Posts: 5
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
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Member variables are implicitly initialized (to zero, false or null), only local variables must be initialized explicitly.
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Alberto,
Instance variables are initilized automatically.
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
Nilesh Deshpande
Ranch Hand

Joined: Jan 11, 2006
Posts: 36
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

Joined: Apr 27, 2006
Posts: 5
Ooops...
I put all inside a main() method.

Thank you.

Alberto
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Cool concept..
Thanks Henry!

-------------
Ravinder
 
 
subject: not initialized