String a[]=null;
if(a[0]!=null)
{
System.err.println("dd-1");
}
if(a!=null)
{
System.err.println("dd-2");
}
}
In first if condtion its throwing null pointer exception, but in second if condition its not throwing null pointer exception? can anyone please explain me the same? is there is any concept behind this on Heap memory allocation.
also i got to know the problem becauese of Missing memory allocation,String a[]=new String.[10]; please expalain the concept?
In the first if condition you are trying to access the first element in the array, even thought there is no allocation done for it.
In second condition you are just testing the reference.
(Think of it as C pointers, char *str = NULL does not allocated any thing except a pointer pointing to NULL)
No memory is allocated for the array only reference is created.
Memory to hold one String object is created on the Heap and
apoints to the allocated string object.