JavaRanch Home    
 
This page:         last edited 16 February 2014         What's Changed?         Edit

Strings Are Bad   

(Level: beginner)

Actually, they aren't, but they're often used badly; especially by beginners who use them as substitutes for other types - and that's what this page is about - because for that, they ARE bad.

The reason it happens is, I suspect, because Strings are usually the first Java class we learn about, and therefore the most familiar. They can also contain pretty much anything you care to put in them, which often makes them seem like an ideal placeholder, BUT THEY'RE NOT.

Here's a classic declaration from a beginner's program:

String colour = "Green";

Now, it may look perfectly reasonable to you; but to an experienced programmer, it's just plain wrong - and on many levels.

'Why?', you ask.

Firstly, because a String is NOT a colour; it's a set of characters (in the above case: 'G', 'r', 'e', 'e', and 'n'). And that's all it is.

A String like "127" might look like a number, but it isn't one. Just try adding two of them together and see what happens. At best you'll get an error; at worst, you'll end up with "127127".

Secondly, Java already has a perfectly reasonable class called Color, which encapsulates some of the aspects of a genuine colour. Indeed, the Color class even has a constant called 'GREEN'. And if it doesn't cover what you want, make your own class. That's what Object-Orientation is all about.

Now obviously I don't mean that you should never use Strings. For a name, or a film title, or a piece of text, it's plainly the right type to use. But don't write:

String startDate = "1/9/2012";
unless you want your senior colleagues throwing up all over your computer screen.

Remember the Mantra - Objects: GOOD; Strings: BAD.


INPUT

Often, a program will need to receive input in String form - either from a file or from the console; or maybe even from a web page - but in such cases, you should convert them to an appropriate Java type as soon as you possibly can.

If they're numbers, convert them to a number; if they're "true" or "false" (or "yes"/"no") convert them to a Boolean; and, most important of all, if it's a String containing information about a customer, convert it to a Customer object. And if you don't have a Customer class? Make one.

Yes, it takes a bit more writing - and, more importantly, a bit more thought - but your program will be a lot better for it.


EXAMPLE

A common trap that newbies fall into is to use Strings so that they can hold values they want to display. This is a very poor reason to use them, especially as all objects have a toString() method, which is designed specifically for this purpose.

A recent example came from a poster who asked how to fill a 2-D array of 'aircraft seats' with "-" so that he could display them like:

- - - - X -
- X X - - X
X - - - - X
...
where "-" denoted an empty seat, and "X" a 'reserved' one.

His 'seat' array was duly defined as:

String[][] seats = new String[20][6];

(or something very similar) and he wanted to write a pile of code to initialize each seat to "-" and then change it it to an "X" when it was reserved.

Not so terrible, you may think; but it is fundamentally flawed, because it makes each seat a procedure rather than what it should be - an object (Java is an Object-Oriented language, remember). It also indicates a basic misunderstanding of the difference between what an object is, and what it's supposed to look like.

The answer?

Well, mine was to advise him to create a Seat class; and I suspect that most experienced programmers would do the same. Now, the definition becomes:

Seat[][] seats = new Seat[20][6];

and all of those settings and changes become methods of the Seat class, rather than clogging up space in main() (or some 'application' class).

Doesn't:

seats[x][y].reserve();
seem a lot clearer to you than:
seats[x][y] = "X";  ?

Furthermore, now that you have a proper class, you can add things to it - for example: a field that says whether it's a window seat, or an aisle seat (or neither); or whether it's economy or business class. Try doing that with a String.

And for display? Simple:

public String toString() {
   return this.isReserved() ? "X" : "-";
}


FURTHER READING:

Effective Java, 2nd Edition - Item 50, pp 224-226.



CategoryWinston

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton