Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not initialized

 
Alberto Cancello
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member variables are implicitly initialized (to zero, false or null), only local variables must be initialized explicitly.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alberto,
Instance variables are initilized automatically.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • 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.
 
Nilesh Deshpande
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ooops...
I put all inside a main() method.

Thank you.

Alberto
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Cool concept..
Thanks Henry!

-------------
Ravinder
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic