Recent Posts

Emptiness is a Warm Gun

This post deals with emptiness. It may sound strange, but the term “empty” in a programming language is not regular, especially when we deal with memory cells. A memory cell cannot be empty, it’s not logical. Since memory cells are physical entities, they contain zeros and ones (as magnetic fields). There can’t be “nothing” in a memory cell, this situation doesn’t exist.

On the other hand, as programmer we do use the term “empty” regarding to variables and memory. When we say that, we mean that the memory doesn’t contain a value that we assigned ourselves. Our attitude for this imaginary emptiness depends in the context of the program we write.

But the situation becomes more complex when it comes to objects. The term “object” may refer to two things – the variable that holds the object’s address and points to it, and the actual object’s data that is located on the heap. So the question is what does it mean when an object is empty? Java dedicated the keyword “null” do indicate the pointer doesn’t point on a object and that means the object is empty.

And the situation becomes even more complex when it comes to strings, maybe the most useful objects in the human-computer interaction. The String class in Java represents a string, and it’s used to create objects of strings.

It’s very common to ask if a certain String variable, represents an empty string. For example, if the user was supposed to input data but nothing was entered. Or a method that should return a message but returned an empty message. Here is a suggestion for a method that gets a strung and returns true if the string is empty and false otherwise:

public boolean isEmpty(String s) {

return s.equals("");


Allegedly it is very simple and logical – an empty string doesn’t contain any character so comparing it to “” would give the answer. But the problem here is that s is an object, so the term “empty” can refer to the object’s pointer itself, if it’s null. For example, if we call this method like that:

String s = null;


We’ll get an exception because we can’t invoke a method on null object.

A better implementation of this method:

public boolean isEmpty(String s) {

return s == null || s.equals("");


Now the isEmpty method covers both cases of string emptiness.

And a short question for you – do you think we could write the return statement in the method like this:

return s.equals("") || s == null;