Strings in Java are a widely used data type (and in most programming languages), they allow us to store anything and everything that has to do with words, sentences, phone numbers, or even just regular numbers. But, as useful as they are, you'll need to know a few important things about Strings before you go wild using them in your code.

String – Object or Primitive?

Strings could be considered a primitive type in Java, but in fact they are not. As a String is actually
made up of an array of char primitives. In other words, if you create the String “Hello World” like so:

String helloVar = "Hello World";

then if you were to look at the helloVar variable in your debugger, you will see the following:

Strings in Java

You can see how the String is made up of an array with 11 elements (0 -> 10) where [0] = H, [1] = e, [2] = l, [3] = l, [4] = 0 etc. Alright, so who really cares that it's made up of an array of chars then right!? Well, I just didn't want you to get confused when you saw your Strings in the debugger ;)

So let's move onto another important fact about Strings in Java, they are immutable! What does that mean? It means that once you create a String, it cannot be changed. So for the example above with my helloVar, if I wanted to change that variable's content, I would have to:

  1. Re-assign a new value into the existing helloVar variable
  2. create a new String object and assign the changed value to it

Okay, so you might be thinking “re-assign into the existing variable… wouldn't that mean changing it? Which you just told me I couldn't do!” Well, what happens behind the scenes is what's interesting, but it will involve me talking about computer memory. Now this isn't the most exciting of all topics, but I'll have to explain it for completion sake, otherwise the professional programmers (also known as ‘geeks') out there will start screaming for attention in the comments section.


This topic is huge, and I could probably write several posts dedicated to this one topic, but to save you from that dull reading, I'll give you a useful summary. You see, all variables in Java will be stored in memory on your computer. The variables will be assigned a location that is identified by an address. Think of the address in memory like your own home address. If you create a new String variable, it will be assigned an address that is currently empty. This is just like if you were to move into a new home, you're obviously not going to move into a home where someone is already living right? Same thing happens with computer memory, Java will figure out where there's some empty memory space and have the new variable “move in” or “occupy” that space. Now, since Strings are immutable, they cannot be changed, and thus, you cannot go to the address where that variable exists and start messing around with it. The only way to change what currently exists at that address, is to assign it a new address with its modified value. The comparison to the real world would be, if you were living in your house at your address with your spouse and two children, but you wanted to have a new child occupy space in your house, you'll have to move into a new house first! It's not the greatest example, but I feel like it works well enough. So what if you want to just replace a String variable that already exists with a new value, what happens then? Well, essentially Java will just demolish your house (clear your current variable's memory) and move you into the house next door (assign the same variable to the next available memory location).

Phew, you see what I mean by a dull subject. All you really need to understand is that you can't change a String in its memory address. Once it's created, it's there until it gets destroyed (also known as Garbage Collected… another huge topic that I won't cover right now). So, let's move onto something more interesting…

String Concatenation

One more fun thing you can do with Strings in Java is String concatenation. What does that mean? It just means that you can put a bunch of Strings together to make one bigger String. Here's an example:

String str1 = "Hello ";
String str2 = "World";
String str3 = "!";

System.out.println(str1 + str2 + str3);

You see those plus + signs between the three variables in the System.out.println? That is string concatenation at work. What it's saying is to “append” the second string to the first, and then “append” the third string to the second. Whats the result?

Hello World!

The usefulness of this may not be apparent at the moment, but one real world example I can think of is when you have your first and last names stored in two separate variables. You've probably seen this many times on the internet. You'll fill out a form of some sort to sign up for a mailing list perhaps… then you'll receive an email later that has your full name printed. This is an example of String concatenation. Example:

String firstName = "Trevor";
String lastName = "Page";

System.out.println("Hello there " + firstName + " " + lastName + ".");

This would output Hello there Trevor Page. You see how we not only put the plus + sign between the variables, but we also threw in some Strings that weren't assigned to variables? We can do this, because whenever you put letters/numbers between those double quotes “”, Java will interpret it as a String, and will allow you to concatenate it to your other String variables! Neat :)

The final thing I want to mention is that you can get yourself into some trouble when trying to compare two Strings to each other. For now I will just say that you need to use the equals() method whenever comparing two Strings. The reason for this, I will be explaining in my next post, as it relates to Objects in general and not just Strings!


Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.