Zach Cochran
by Zach Cochran
3 min read

Categories

Tags

Today we’re going to talk a little about strings.

I had initially been trying to plan out something that was much larger than this, but after more consideration I decided that I wanted to keep this short and better scoped. Sometimes I worried that I’m too close to going off on tangents instead of just talking about what I learned about. Like i’m doing now…

Some Things About Strings

Strings are pretty basic conceptually, right? They’re just an array of chars. Nothing to crazy. But there were a few things I learned about them that I didn’t know before while going through some of my lessons. This is going to be short, brief, and with very little code examples.

1. Strings are Immutable

I had always heard that this was the case, but I never really understood what it meant. Essentially, strings cannot be changed once they’ve been initialized. As outlined in this post much better than I ever could, strings end up being placed into a specific set of memory. When a string is being created, a lookup is done to see if the string already exists somewhere in memory, so the reference can point to that location. If not, then we create a new one.

What that means is that when you do something like the following:

public void stringReassignTest() {
   String a = "Test";
   a += " case";
}

While it looks like we’re just reusing a over again, we’re actually reassigning the reference over to the string. So when we do the append in this case, we’re actually creating another string in memory and then pointing to it.

Like I said, check out that link above, it does a great job of explaining this.

2. Strings are UTF-16

Java strings will support just about any characters you throw at them. UTF-16 is a heck of a lot of languages, and that support is just native.

3. Using String Builder

Since strings are immutable, Java gives you a way to actually build out your string before you initialize anything. This creates less clutter, and provides you a more methodical way for constructing strings. Personally I think it looks really ugly and makes it hard to read, but it does a lot of things for you, so I guess that’s cool.

Here’s a super basic example of what a stringbuilder will allow you to do:

public void stringBuilderTest() {
   StringBuilder myString = new StringBuilder();

   String name = "Bob";

   myString.append("Hello there, ");
   myString.append(name);
   myString.append("!");
   myString.toString();

   System.out.println(myString);
}

The print statement above will print out “Hello there, Bob!”.

The important thing to remember about using StringBuilders is that your string will not be created until you call the ending toString().

4. valuOf()

You can use the String method valueOf to convert something like a number to a String. This is handled automatically for you when you do something like concatenating values with a string, but is something you will have to explicitly do if you’re trying to get it converted.

To handle the conversion, you just need to stick your value into the valueOf method, and poof!

public void printNumbersTest() {
   Long l = 12345567889l;
   System.out.println(String.valueOf(l));
}

Note that this is really only when working for numbers. For Object values, you’ll need to use…

5. toString()

In order to get objects into strings, you’ll need to write a toString() method specific for your class. If you just use the toString that is inherited from the base Object class, you’re just going to be getting a representation of the object and where it was first created in memory. Not very useful.

Instead, you’ll want to create your own toString() so that something meaningful can be returned back.

Conclusion

That’s all I really wanted to say about Strings. Like I said at the beginning, I had initially planned out more I wanted to do with it but just generally wasn’t interested enough to put more into it. As always, code is located in my github repo.

💚 A.B.L.