• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

local arrays

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this question from the bonus exam of K&B.

5. Given the following,
1. public class Test {
2. public static void main(String [] args) {
3. long [] [] a1;
4. long [] a2 [];
5. a2 = new long[3][];
6. a1 = a2;
7. System.out.println(a1[1][0]);
8. }
9. }
what is the result? (Choose one.)
A. 0
B. null
C. Compilation error at line 5
D. Compilation error at line 6
E. Compilation error at line 7
F. An exception is thrown at runtime

Why B is not the correct answer? Will the answer be B if we replace the line 5 with
a2 = new long[3][3];

Thanks in advance.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boss for primitives it will not give null value
and method local array variables are not initialized by default
when u try to retrive the value which is not initialized it will give a null pointer exception
correct me if iam wrong
If it is not clear just give me a mail
thank you [email protected]
thank you
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

Boss for primitives it will not give null value
and method local array variables are not initialized by default
when u try to retrive the value which is not initialized it will give a null pointer exception
correct me if iam wrong
If it is not clear just give me a mail



Local array objects are also initialized. But In the above program the first dimension is mentioned. But the second dimension is not mentioned. The mentioned dimension values get initialized with default values.

The above program will give raise NullPointerException at runtime.



Will the answer be B if we replace the line 5 with
a2 = new long[3][3];



No. the result will be 0. It will initialize the values....

Regards,
Krishna.
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get that the primitive type array can't be initialized with null. But all local arrays are initialized (assumption that local arrays are not initialized is wrong).
Can anyone explain why the answer is F (runtime exception)?

Gyanesh
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, array elements are given their default values (0, false, null, �\u0000�, etc.) regardless of whether the array is declared as an instance or local variable.
It's true that local primitives are not given default values (and any attempt to use them will be flagged by the compiler).
However, arrays are objects and not primitives and their elements are ALWAYS given default values.
Also, when declaring a multi dimensional array, only the leftmost dimension(s) need be specified.
Eg:
int myarray[][] = new int[4][];// Fine. Compiler is happy.
int myarray [][][]= new int[5][][];// Fine. Compiler is happy.
int myarray [][][]= new int[3][][9]; // ERROR!! declared dimensions must be leftmost.
int myarray [][][][][]= new int[5][6][7][][]; // Fine. Compiler is happy.
int myarray [][][][][]= new int[5][6][][8][9]; // Uh-oh!! Not good.

And since multidimensional arrays are arrays of arrays, the size of each array is under your control (called irregular or uneven arrays).
And any inner arrays whose sizes are not defined are null and hence trying to access them results in a NullPointerException.
Sashi
 
author
Posts: 23959
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
It is *not* the println() method that is throwing the exception -- that method is smart enough to print "null" when you pass a null string variable.

What is happening is that you are not passing a string, you are trying to pass a long. The exception is thrown when Java is evaluating the "a1[1][0]" part of the expression.

Henry
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try running this program:
public class arrays{
public static void main(String args[])
{
int myarray[][] = new int[4][];
System.out.println(myarray[0]);
// System.out.println(myarray[0][1]);
}
}
See the output.
Now, uncomment the above commented line and run it again.
Now, Gyanesh, can you tell us why you would get a NullPointerException?
Sashi
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the great explainations Shashi and Henry.
I'm sure Raghav and Hari has got it too. Just because an array is of primitive type, it does not mean that all the levels of arrays contain primitive type.
In this example, a1[0], a1[1] and a1[2] refer to an integer arrays (ie int[]). So they are initialized to null.
Since a[0], a1[1] and a1[2] are integer array types that are not initialized, accessing an element results in NullPointerException.

Guess what would be the result of this program:

ublic class arrays {
public static void main(String[] args) {
int[][] a = new int[3][2];
System.out.println(a[0]);
System.out.println(a[0][1]);
} // end of main
} //end of class

It prints:
[I@ba34f2
0

Since all dimentions of the array are specified during instantiation, both the arrays (the array of int array and the int array) are initialized. I should say the first dimention is not only initialized, it is assigned the addresses of the initialized int arrays.

Correct me if my explaination is wrong.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic