Java Strings

What are Strings

I've mentioned them before, but to refresh your memory, a Java String is used to store letters and numbers in one variable type. Strings are extremely common in Java code and are mostly used for things like user inputs (i.e. website login information – username/password).

You create a String by putting double quotes around a word or sentence. This is called a String literal. An example of the creation of a String would be:

String myString = "Hello World";

Strings are Immutable

This means they can't be changed, so once you create a String, you're stuck with it. If you need to modify the String, what will happen is you will need to create a new String and replace the existing one with your new String.

There are a bunch of reasons why Java made Strings immutable, but one good reason is to save memory. When Strings are created (via String literals) they get placed in a pool (if one didn't previously exist in the pool). If you try to create another variable with the same String literal, then it just grabs the one in the pool. This is the same idea of a lazy loaded singleton (if you know what that means).

Here's a great explanation from StackOverflow:
http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

What can we do with Strings

Lots of things! You can concatenate 'em, truncate 'em, search 'em, search and replace 'em, uppercase 'em, lowercase 'em, and more it's almost like having your own word processor in Java. All of this functionality that is granted to us makes dealing with Strings amazingly powerful. What I mean by that is, if someone in the real world were to give you a project to work on and code, I guarantee you that you'll be doing someone with String manipulation. That being the case, aren't you glad you're learning all about it? Let's dive into some of the more common uses of String manipulation shall we?

String Concatenation

String concatenation is the process of mashing two Strings together to form one larger String. A great example of this would be if we have a User object, and this user has a first name and last name. First and last names are stored in two separate fields as Strings. Let's say we have a requirement to send an email to all our Users, we want to start our email with “Hi (first name) (last name)”, we would use String concatenation to accomplish this feat. Mash first and last name together to form the greeting in the email. In code, this is just done by using the plus (+) sign, so you'd have firstName + lastName. You can also concatenate String literals with String variables using the same plus (+) sign. Our example would contain one String literal and two variables "Hi " + firstName + " " + lastName.

String Truncation

I use the word truncation loosely, because there's no way to actually truncate a String, because they're immutable. What you can do though, is identify which part of the String you do want to use, and assign that to a new String. The method we use for this is called substring(). It allows us to say, grab the first 10 characters of a String, or go 5 characters into a String then grab the next 10 characters of that String. One common use of substring() that comes to mind is when I'm trying to build a List of numbers separated by commas. You'll iterate through the list and append a comma after each number, all the while assigning this big list to a String. Well, when you're done, you'll usually have a nice comma separated List of Strings with a comma at the very end as well. Well, I just want to chop that last comma off, so I use the substring() method to do that… I grab every character from that List, except the last one! Voila, comma separated List, perfectly formatted :)

Search or Search & Replace

It's exactly what it sounds like, you have the ability to search through a String for another String. This is done with the indexOf() method. It works in a bit of a strange way, you see you search through one String for the index of another String. Which means, if the word or character you're looking for is found, it will return the index of that occurrence (i.e. an integer), otherwise it will return a -1. Not exactly as helpful as a true or false if your search String was found, but it'll do. And to take it one step further, you can actually replace a matched String with another String if you like… just use the replace() method. It's just like using find & replace in word or notepad!

Word of Warning

So there's lots of things that you can do with Strings, but there's one issue that comes up with the usage of Strings… it's when you're trying to determine of two Strings are equals to each other or not. This can be really frustrating for new developers, and it stems from the fact that Strings are immutable. If you use the standard == operator to compare two Strings it may or may not work properly, and that's because == operator checks to see if the two Objects are actually the same Object, and doesn't check to see what the value of that Object is. The trick with Strings is to always use the equals() method when comparing. The equals() method checks the value of the Object, not the physical instance of that Object.

Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.