KBD

Keith Devens .com

Thursday, December 4, 2008 Flag waving
The very name "selection" implies that you're choosing between two or more variants. So that means that the end... – Dr. Walter Veith
← Quickies →

Daily link icon Saturday, January 11, 2003

Java and Pointers

Jeremy: java.lang.NullPointerException

I remember back in the mid-90s when Java first came out. I actually read the Java 1.0 language spec. The whole thing. It was cool. One of the folks who was telling me about it (just before I read the spec) endorsed it by saying "it's like a cleaner C++ with no pointers!" I've heard the "you don't make pointer mistakes in Java" bit a lot over the years.

Despite all that, I'm still able to convince Java to throw a java.lang.NullPointerException at me. Every time I get one (which is pretty often right now) I have to laugh just a bit because this is the a language without pointers.

Charles: NullPointerException:

Jeremy Zawodny has discovered the NullPointerException, and laughs because he's been told that Java is "the language without pointers". Claiming that Java is a language without pointers is, of course, rubbish. All variables that refer to objects are references (i.e. pointers) to that object, which resides somewhere in the heap.

What Java lacks, is pointer arithmetic. The only operation that is permitted on a pointer is dereferencing (the '.' operator), which allows you to perform actions on the object being pointed to. There are two reasons for this. Firstly, it frees the JVM to do memory-management how it wants, and not have to be a slave to the "pointer equals memory address" thing. Secondly, and more importantly, it prevents a large class of memory-corrupting, security-destroying, application-crashing bugs that can result from direct manipulation of pointers.

Which brings us to null. Null is the value for a reference which means "this reference does not point to any object". If you try to dereference null, you get a NullPointerException.

Some languages treat null differently. Objective-C's nil is a reference to the null object, which responds to any message you throw at it with nil. This makes an unassigned reference something like a black hole, you can send anything in but you'll get nothing back. Smalltalk gives you both worlds: nil is a singleton that passes every message to doesNotUnderstand. In development environments this throws up the debugger when it's reached, but common practice is to redefine nil to observe the Objective-C behaviour in production.

Fascinating, I never knew that.

Also check out a little more on this by Kasia.

Update: read more on LtU. Ehud makes the point I was thinking as I wrote this post, and he says it well:

It should be noted that this issue is quite relevant to teaching programming. Some people assume that by using language like Java you eliminate the need to teach students about pointers. This is a rash conclusion.

← Quickies →

Comments XML gif

Stephan Schmidt (http://snipsnap.org) wrote:

Nice is a nice language which uses Java and compiles to a VM. By default a reference in Nice cannot be null, so NPEs anymore. If you want nullable references you have to declare a reference nullable.

∴ Stephan Schmidt | 14-Jan-2003 8:46am est | http://snipsnap.org | #1267

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 4 posts

Recent comments XML

Girls, please don't get breast implants

I have 34 A breast but at 22 years​old they seem to be growing again​which ...

76.64.120.153: Dec 3, 10:00am

Perl 6 1.0 in March?

Doh, my mistake. I'm aware of the​relation between Parrot and Rakudo​but I'...

Keith: Dec 2, 1:03am

Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Generated in about 0.2s.

(Used 8 db queries)

mobile phone