Skip to content

Variables and Strings#

We have talked about Variables and Numbers extensively, so let's now cover Text, too! Of course a computer can store text somehow in its memory, so it's logical that you can make use of this in programming.

String greeting = "Hi!";

This works mostly the same as with other variables, too. We declare it with its type. The type is String. We then give our variable a name, this one is called greeting, but that could be any other name, too. In that line, we also chose to initialize our variable. We're setting its content to "Hi!".

Strings in Java are always contained in double quotes ("). Let's use our new variable somehwere:

String greeting = "Hello from Processing!";
println(greeting);

Or let's draw this on the canvas.

size(300, 200);
String greeting = "Hello from Processing!";
fill(0, 0, 0);
text(greeting, 20, 20);

Note The Difference!

When you use a variable, it does not go into quotes itself! These two are very different, please try both in your Processing IDE:

String name = "Claudius";
println(name);
String name = "Claudius";
println("name");

Try it in your Processing IDE and make sure you understand the difference!

What does "String" mean?#

In programming, "String" usually means "a couple of characters" and is often used for text. It could also contain a LOT of text, of course. In your computer's memory, there will be some place where all the variables of your program are stored. Your program keeps track of memory addresses (fancy name for a special kind of number). Some of these variables contain text, and the variable points to the start of this text. Following this start, there will be a literal string of characters.

Screenshot from a hex-editor showing some (mostly) ASCII text

Screenshot from a hex-editor showing some (mostly) ASCII text, but including some annotations what these numbers all mean.

As we discussed when we talked about Encoding, these bytes in the middle could be anything. But if we know that this is UTF-8-encoded text, we can start reading it. We might even find out what the hidden emoji is!

Strings vs Numbers#

Why don't we put all our variables into strings then, if they can do so much more? Well, once a number is inside of a String, it can't do some of the things as easily any longer. If we put a number into a string, we keep a printable version of that number. But that printable version is not easy to use in calculations.

int age = 25; will store the bits 00000000 00000000 00000000 00011001 in memory. That is still a number (only written down in binary) and we can do all the things we're used to with numbers, like adding, multiplying, comparing and so on.

String age = "25"; will store 00110010 00110101 in memory. That is the ASCII code 0x32 and 0x35 which encode the digit 2 and the digit 5. Regular math rules don't really apply to text, so we can't do any of the operations we could do with the int. If you need a number, use one of the numeric types.

Combining Strings#

Sometimes you need to alter a string slightly, and one very common alteration is combining several strings into one. Let's try this:

three lines of code, combining two strings with a plus sign

Question

Why is there no space?

We could also assign the result to its own variable instead, of course.

While there is a + operator, there are no -, / or * operators.

Using double quotes " in your String#

If a " marks the beginning and end of our string, then how can we ever use " IN our strings, if we need to?

String example = "Putting " in the string won't work.";

You can tell from the syntax-highlighting, that this is not what we wanted, and processing is furious.

processing shows a syntax error because our string is doing odd stuff.

So, no, you can't put the double quotes in your Java program. At all. Or can you? Ok, it would be pretty hilarious if there was some magic symbol that you can't use because it breaks everything. Let me tell you about escaping. Because there sometimes are special symbols, but usually you still get to use them.

String example = "Putting \" in the string won't work unless you escape it.";

processing works fine if that double quote is correctly escaped.

There are a few other characters that don't work as expected in a String in Java, and all of them can be escaped in a similar way. The Java Language Specification has a list of "Escape Sequences", but the most important ones are probably \\(1) for the backslash and \n for a "newline" character.

  1. ironic, isn't it? We used \ solve a problem, but now we need \\ to solve the problem with \ itself.

Single characters in Java#

There's another data type for text. "Text" is a little bit of an exaggeration. char is a type that can store one single character. For single characters, we use the single quotes: '.

char favoriteLetter = 'F'; // press favorite letter to pay respects

This one doesn't come up a lot, but you will need this for keyboard input later.