Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can new vector be null ???

 
Arun Thakur
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have :

Vector vet = new Vector(10);

Can items of vet be null at this point ???
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No the vector has no items in it. The parameter to the constructor is simply the initial capacity of the Vector, but when it's created by that constructor, it is empty.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Thakur:
I have :

Vector vet = new Vector(10);

Can items of vet be null at this point ???


Depends on your definition of "items".
No, you cannot observe "items of vet to be null" through the contract of the type, however, if you were running in a debugger, you'd likely observe a field (which is of course, not exposed directly through contract) of an array type that contains null elements.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
Depends on your definition of "items".
No, you cannot observe "items of vet to be null" through the contract of the type, however, if you were running in a debugger, you'd likely observe a field (which is of course, not exposed directly through contract) of an array type that contains null elements.


Unless of course you add null elements to the Vector in which case you could. Though I presume they're asking if the new Vector will have 10 null elements in it. The answer is, of course, no. As Tony said the underlying implementation will have an array with null values, but that is irrelevent. As far as the Vector is concerned it has a size of 0 and attempting to retrieve from it will result in an exception. You'll have to actually add an element yourself. You can add null if you like, but I don't know why you would.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic