Logout

Strings as Objects vs Literals, & String Equality vs Identity

Strings are strings of characters. And so a Java string is an array of char primitives. And so a string is actually a data structure, rather than a primitive. So if you wanted, you could declare an array of chars every time you wanted to work with words. But this would be pretty silly. So the makers of Java decided that since strings are so commonly used that they would give them the ability to be treated as literal primitives

But the makers of Java also saw strings as something which could be nicely be controlled by a specific String class, which could have pre-made attributes and methods that are commonly used when working with words. Attributes such as length could be kept track of and directly used, and methods such as substring for accessing a subset of the character array could also be pre-implemented and available to be easily used.

And so the Java String is a very unique thing within the world of Java. It is actually a structure made up of primitive chars, yet it can itself be declared and used as a primitive, and it can also be declared and used as an object of a class, namely the String class.
- when you just want to use a word to be displayed, and will do no special processing of it, you'll just declare it as a String literal.
- when you want to take advantage of the object possibilities of Java, along with particular methods, you'll delare it as a String object.

String as litteral:

String x = "hello world";

String as object:

String y = new String("hello world");

The two Strings above have equal "equality", since they are both the same words. But their "identity" is differenent, as they will be differnt places in memory. In fact y is a reference to the String object, whereas x is the memory address starting at the literal hello world.

Later on maybe I'll give a detailed theoretical written explanation of all this, but the best way to understand the difference between a String literal and a String object would be to take a look at a good diagram of the workings of both at the memory level.

Furthermore, the Morelli "Java, Java, Java" textbook does a very good job of dealing with the difference. The String chapter starts on page 305.

 

(One other minor thing simply to note at this time is that Strings are "immutable", meaning that (similar to the way that an array cannot grow) once they are made, they cannont be changed. It may seem as if you can change a String with lines like:

String x = "hello world";
x = "hello world extended a bit";

But what is actually happening here is that the first array of chars h e l l o w o r l d gets "orphaned", and the variable x will create a new literal string of chars another place. Sooner or later Java's garbage collection will realize that the first array of chars is not being pointed to by anything, and so will "mop it up", but it is a bit inefficient in terms of memory usage. This is particularly true in any loop which seems to work with the same string loop after loop, when actually it is instantiating a new String every time through loop. This isn't that big a deal, and the IB curriculum donesn't worry about it, but for what it's worth, there is one way around this, and that is to use the Java method StringBuffer instead. See Morelli page 318 for more information on this class which is more memory friendly than the String class.)