Is null an object?

Is null is an Object if yes then why can't we use equals method on it. Why null.equals(null) gives error and null == null gives true.
[148 byte] By [rd81a] at [2007-10-3 4:02:14]
# 1
null is no Object, that's why null.whateverMethod fails, including equals(). null == null doesn't fail because it is a simple evaluation without any methods being invoked.
PhHeina at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

JoachimSauera at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 3
Sorry..but could u pls be more elaborative on ur statements..I havent understood it..thanks
JK_JAVAa at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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].

JoachimSauera at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 5
Why we can add null value into Vector When vector takes only Objects.
rd81a at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

JoachimSauera at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

JoachimSauera at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 8
I agree with u but I have one doubt that when we write code like thisv1.addElement(new String("Hello"));Here we are storing object into vector not reference.Plz help me
rd81a at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 9

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!

JoachimSauera at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 10
> 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.
CeciNEstPasUnProgrammeura at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 11
> Sorry..but could u pls be more elaborative on ur statements..*ROFL*
CeciNEstPasUnProgrammeura at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 12
@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.
CeciNEstPasUnProgrammeura at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 13

> > 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.

JoachimSauera at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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

oxbow_lakesa at 2007-7-14 22:01:37 > top of Java-index,Java Essentials,Java Programming...
# 15

> @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.

kilyasa at 2007-7-21 10:22:16 > top of Java-index,Java Essentials,Java Programming...
# 16
> 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 Oh lord, that reminds me of a fight I had with a math professor over whether Zero was a number...
TuringPesta at 2007-7-21 10:22:16 > top of Java-index,Java Essentials,Java Programming...
# 17
Please somebody t ell me if i can add null value in vector and how?
pintu.inuvaa at 2007-7-21 10:22:16 > top of Java-index,Java Essentials,Java Programming...
# 18
OMG
ejpa at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 19
> Please somebody t ell me if i can add null value in> vector and how?Why don't you try the following code and check?Vector v = new Vector();v.add(null);
aniseeda at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 20
> Please somebody t ell me if i can add null value in vector ...You? I doubt it.
prometheuzza at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 21
> You? That should be u
aniseeda at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 22
plz feed me a stray cat
georgemca at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 23
> plz feed me a stray catThen u can strut.~
yawmarka at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 24
Brian Setzer for President of the World
georgemca at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...
# 25
> Brian Setzer for President of the WorldThat makes me want to jump, jive, and wail.~
yawmarka at 2007-7-21 10:22:17 > top of Java-index,Java Essentials,Java Programming...