Null Object

M

Moon2

Can anyone please tell me why the following doesnt create a runtime error.

Object s = null;
System.out.println(s);

The output is null. Why doesnt it call the Object's tostring method and
throw a null pointer exception. Any comments will be appreciated.

Moon2
 
S

Stefan Ram

This is the result of a a design decisions.

I meant »This is the result of a design decision.«.

Usually, when null is not a valid value, one wants a receiver
of the value to mark failure as early as possible, e.g., by
throwing.

But it makes sense for some receivers to accept null as a
possible value. This depends on the context.

In the case of »println«, it is often more helpful to show the
null than to abort the action, e.g., when used for debugging.
 
M

Mike Schilling

Moon2 said:
Can anyone please tell me why the following doesnt create a runtime error.

Object s = null;
System.out.println(s);

The output is null. Why doesnt it call the Object's tostring method and
throw a null pointer exception. Any comments will be appreciated.

Because the engineers who defined the method deemed that to be less useful
behavior that what it actually does. Note that this isn't a general
language issue, it's specific to OutputStream.println(Object ). If you'd
asked about

Object s = null;
String msg = "The answer is " + s;

Now it's a language issue, though the answer remains pretty much the same.
 
O

Oliver Wong

Moon2 said:
Can anyone please tell me why the following doesnt create a runtime
error.

Object s = null;
System.out.println(s);

The output is null. Why doesnt it call the Object's tostring method and
throw a null pointer exception. Any comments will be appreciated.

System.out.println() doesn't actually directly call the passed-in
object's toString() method. Instead, it calls String.valueOf(s). The
implementation of the String.valoeOf(Object) method is:

public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}

That is why you see the output "null" instead of an NPE.

- Oliver
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Moon2 schreef:
Thanks very much for clearing things up

By the way, note that your subject is misleading: this is not about a
Null Object (there is non in the standard Java API), but about the null
*reference*.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFGAQFge+7xMGD3itQRAjltAJwJ5ADS9SkcSySBObKW2xXGJ7QzIQCfdDBY
xXRZK9uLUoMHI05HWIA5pKc=
=HspG
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top