Null is not an object. It is the only non-Object value that is valid for a reference variable to hold.
See what the [url=http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#29375] JVM Specification[/url] and the [url=http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.7]Java Language Specification[/url] say about null.
> Sorry..but could u pls be more elaborative on ur
> statements..
Could you please spend the extra seconds to type out words such as "you", "please", "your"? It greatly helps readabilty, especially for people who's native language is not english.
And I assume you haven't read my answer yet (or haven't followed the links) or you should have at least some more detailed questions than "I don't understand."
And you could also read [url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url].
> Why we can add null value into Vector When vector
> takes only Objects.
If Vector only took object then we couldn't. But Vector doesn't take objects, it only takes references to objects. (Hint: you can never, ever pass an object! You can only pass references to objects or primitive data types).
And since null is a valid reference (although one referencing no object) it can be passed to the Vector and stored in there.
Think about it that way:
Basically you can do 3 things with a reference:
- pass it to another method/constructor
- store it in a variable
- dereference it (i.e. access the object that's referenced, call a method on it or get a variables value of it)
With the null reference you can do the first two things just fine. But if you try to dereference the null reference you will get a NullPointerException.
First of all: Never write new String("Hello")! It's useless and creates superfluos objects.
And: No! You don't store the object in the vector. You only ever store the reference inside the fector. You can later retrieve that reference and use that reference to access the object. But the object itself is not stored in the Vector, it can't be!
> > First of all: Never write new String("Hello")!
> It's
> > useless and creates superfluos objects.
>
> Not that using a Vector is too much better, in most
> cases.
Agreed, I didn't want to go into the "advanced" stuff ;-)
@OP: You could read [url=http://www.javaranch.com/campfire/StoryCups.jsp]this story on references[/url] and (since I'm sure the question will come up) [url=http://www.javaranch.com/campfire/StoryPassBy.jsp]this story on passing references by value[/url]. They are pretty good at explaining the concepts.
> I agree with u but I have one doubt that when we write code like this
>
> v1.addElement(new String("Hello"));
> Here we are storing object into vector not reference.
> Plz help me
You may just have to accept that at the moment you don't understand what a variable of reference type is, in which case you maybe aren't really going to be able to follow the finer points as to exactly what null is.
So maybe it's easier to think in terms of, erm, stuff. You can call methods on stuff. Objects are stuff, null isn't. null is the concept of no stuff
> @OP: null is a special type that can fit any
> reference. So that's why you can pass a reference
> containing it to a Vector or any other method.
Just a small correction, some collections donot accept null. One example being HashTable. While HashMap does accept nulls.