Java String Variables

Welcome back to the second in our series on Strings in Java. If you’re coming in to this page first or would just like a quick refresher on what a string is, you can view the first post in this series.

What is a variable?

In programming a variable is a reference to some bit of information.

Take a look at the following snippet.

int x = 37;

We are creating a variable called x that contains the value 37. The intin front of the variable name tells Java that xis an integer primitive.

Defining a String variable

Defining a string variable works the same way in Java. We just have to tell it that it’s a string.

String x = "Hello";

Now xis a String and not a int.

One important little caveat though. x isn’t actually holding the letters for Hello. It’s holding a memory address of where Java can find the string. That will be important later when we talk about the equals method.

Variable Naming

We do have to follow a couple of rules when naming a variable in Java.

  • First character must be a letter, underscore or dollar sign. Generally though you want it to be a letter.
  • After the first character you can also include numbers. You just can’t use a number in the first spot for a variable name. var2 is a valid variable name. 2varis not.
  • Variable names must be distinct within the same scope.

In Java you’ll also typically use what’s called camel casing. In camel casing you start the name with a lower cased letter. If there are other words in the name you capitalize the first letter of each. Since you can’t have spaces in variable names it gives the variable name separation between words.

So if you wanted a variable to store the name of your best friend you might name it myBestFriendsName.

Creating String Variables

There are two ways to create a new string variable.

First one is an old style, and not seen very often. Since a String is an object and has a constructor, we use that to create a new string.

String firstString = new String("Hello");
String secondString = new String("Howdy There"); 

The second is closer to what other languages do and is what you’ll see more often.

String firstString = "Hello";
String secondString = "Howdy There";

Notice in both cases we had to tell Java that firstString and secondString are string references.

99% of the time these are interchangeable and it’s shorter and easier to use the second method. The only time the difference matters is when multiple variables contain the same string.

Consider the following code snippet.

String first = "Hello";
String second = "Hello";

String third = new String("Hello");
String fourth = new String("Hello"); 

Java tries to be efficient. It’s going to notice that first and second have the exact same string. When second is defined Java will know that there’s already a variable pointing to "Hello" and point second at the same place.

third and fourth are different. By creating a new String those two lines specifically tell Java not to use the same reference and puts new copies of "Hello" out in memory.

This is why you have to use the equals method when comparing strings in Java. Comparing using two equals signs compares the memory addresses and doesn’t always give you the answer you expect.

But what about when you need to put two or more strings together? We’ll cover that in the next post on string concatenation in Java.

Coding Practice

Time for a little exercise. For this one you’re going to create a string variable named s that contains the string "Hello There" and another named t containing "This is Java!". Your code should go just below the comments after public static void main(String[] args) and before the System.out.println() lines.

public class Example {
    public static void main( String[] args ) {
        // Define your variables below this line
        String s = "Hello There";
        String t = "This is Java!";
        // ... and above this one
        System.out.println( s );
        System.out.println( t ); 

In this series

A quick, short introduction to strings in Java.
You can't always work with string literals. You need variables.
Concatenation is the fancy word for combining multiple strings.
Built in method to see if two strings are the same.
Need to know how many characters are in a string? There's a method for that.
indexOf / lastIndexOf

Leave a Reply

Your email address will not be published. Required fields are marked *