Null object

From Rosetta Code
Revision as of 21:21, 30 July 2008 by rosettacode>Mwn3d (Created task with a few examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Null object
You are encouraged to solve this task according to the task description, using any language you may know.

Null (or nil) is the computer science concept of an undefined or unbound object. Some languages have an explicit way to access the null object, and some don't.

Show how to access null in your language by checking to see if an object is equivalent to the null object.


C

C's access to null is by way of a macro which simply evaluates to 0. <c>if(object == NULL){

  printf("object is null");

}</c>

C++

C++'s access to null is (as in C) by way of a macro which simply evaluates to 0. <cpp>#include<stdio>

if(object == NULL){

  cout << "object is null";

}</cpp>

Java

<java>if(object == null){

  System.out.println("object is null");

}</java>

Ruby

if object == nil

  puts "object is null"

end