• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt in Arrays(Self Test Question)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In page number 50,Question 16

public class TestDogs {
public static void main(String [] args) {
Dog [][] theDogs = new Dog[3][];
System.out.println(theDogs[2][0].toString());
}
}
class Dog {}

The answer for this question is given as
An Exception is thrown at runtime......

but as theDogs[2] is not initialized as
theDogs[2]=new Dog[10];

Y isnt it giving a compile time error saying tht the 1D array(theDogs[2]) is not initialized???
Y is it giving a NullPointerException at runtime
[ July 31, 2007: Message edited by: Lavanya Lingam ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lavanya Lingam:
In page number 50,Question 16

public class TestDogs {
public static void main(String [] args) {
Dog [][] theDogs = new Dog[3][];
System.out.println(theDogs[2][0].toString());
}
}
class Dog {}

The answer for this question is given as
An Exception is thrown at runtime......

but as theDogs[2] is not initialized as
theDogs[2]=new Dog[10];

Y isnt it giving a compile time error saying tht the 1D array(theDogs[2]) is not initialized???
Y is it giving a NullPointerException at runtime

[ July 31, 2007: Message edited by: Lavanya Lingam ]



theDogs is a reference to an array. The elements of the array are arrays of Dog objects.

theDogs has been initialized so it points to an array of 3 objects, but since each element in theDogs is a reference to an array, which is an object, initialially the references are null.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Lavanya Lingam,

Welcome to JavaRanch


Y isnt it giving a compile time error saying tht the 1D array(theDogs[2]) is not initialized???



Since the theDogs variable is an array and by default all array elements are initialized with the default value. The default value varies for each primitive types and for objects it is NULL always.

Hope this helps.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lavanya,

"Y isnt it giving a compile time error saying tht the 1D array(theDogs[2]) is not initialized???"

theDogs[2] is inialised to null. Bcoz theDogs[2] holds reference to 1-D array object & default value for object reference is null.So there will not be any compile time error.


"Y is it giving a NullPointerException at runtime?"

System.out.println(theDogs[2][0].toString());

Since theDogs[2][0] is not initialised, It has default value i.e null & if members are invoked with such object,there is nullPointer exception.

It can be corrected by following code.

theDogs[2][0] = new Dog();
System.out.println(theDogs[2][0].toString());

Hope you got satisfied.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic