Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Emptiness Is a Warm Gun

DZone's Guide to

Emptiness Is a Warm Gun

Let's philosophize on null values for a bit and see how they relate to both coding and physical memory as we consider strings and objects.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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 programmers, we do use the term “empty” with regard 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 that the pointer doesn’t point to 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 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 returned an empty message instead.) Here is a suggestion for a method that gets a string 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:

String s = null;
System.out.println(isEmpty(s));


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

A better implementation of this method is:

public boolean isEmpty(String s) {
    return s == null || s.equals("");
}


Now the isEmpty method covers both cases of string emptiness.

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

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


Well?

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
java ,null ,string ,objects

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}