String Variables

So far, we have have written a programme to print various phrases to an output window. In programming, a sequence of characters is known as a 'String' (it might help to remember it as a 'String' of characters). In the Hello World task, we literally typed in the characters we wanted inside quotation marks, any such string in quotation marks is known as a 'string literal' (it is, literally, a string of characters).

Sometimes it's useful to be able to assign a name to a string, so we don't need to type it out, consider the following example:

System.out.print("10");
System.out.print(" green bottles hanging on the wall");
System.out.print(", ");
System.out.println("10");
System.out.print(" green bottles hanging on the wall");
System.out.print(", and if one green bottle should accidentally fall, there'd be ");
System.out.print("9");
System.out.print(" green bottles hanging on the wall");
Whilst we have copy and paste at our disposal, if we wanted to change the 'green bottles hanging on the wall' phrase to something else, we would need to make the change 3 times. If we have all ten verses of the song, we would need to make 30 changes

A better approach would be to store the string 'green bottles hanging on the wall' using a shorter name that refers to it, a variable and then reference it when we needed it. We could do this as follows:

String wallPhrase = " green bottles hanging on the wall";
This tells the computer that we are creating a String variable, we've decided to call the variable 'wallPhrase', and that it is (=) the string of characters " green bottles hanging on the wall".

Whenever we use wallPhrase, we get the text ' green bottles hanging on the wall'

When we want to use the phrase, we simply refer to it by name (without quotes), for example:

String wallPhrase = " green bottles hanging on the wall";
System.out.print("10");
System.out.print(wallPhrase);
System.out.print(",");
System.out.print("10");
System.out.print(wallPhrase);
System.out.print(", and if one green bottle should accidentally fall, there'd be ");
System.out.print("9");
System.out.print(wallPhrase);

It is important to note that variable names (such as wallPhrase) in the above example cannot have spaces in them. The convention is that variables made up of two or more words use lowercase for the first word, and then capitalise the first letter of every subsequent example. This is known as 'camel case'

Task

  1. Create a new command line project (using the same method described in steps 1-9 of the New project task). Ensure the project is named GreenBottles.
  2. Make a note of the location of the project (and subsequent projects) as this, and others are often used again in later tutorials.
  3. Add the code to the project (identical to the first block of code in the example above) as shown here:
    package com.company;
    public class Main {
        public static void main(String[] args) {
            System.out.print("10");
            System.out.print(" green bottles hanging on the wall");
            System.out.print(", ");
            System.out.println("10");
            System.out.print(" green bottles hanging on the wall");
            System.out.print(", and if one green bottle should accidentally fall, there'd be ");
            System.out.print("9");
            System.out.print(" green bottles hanging on the wall");
    }
    }
    Run the code to check it works as expected.
  4. Make changes to the code you just added, so it appears as shown here:
    String wallPhrase = " green bottles hanging on the wall";
    System.out.print("10");
    System.out.print(wallPhrase);
    System.out.print(",");
    System.out.print("10");
    System.out.print(wallPhrase);
    System.out.print(", and if one green bottle should accidentally fall, there'd be ");
    System.out.print("9");
    System.out.print(wallPhrase);
  5. Run the code again and check it works.

Task

  1. Create a new command line project, this time naming the project 'Happy'
  2. Add the following code, taking care to correctly include spaces in the strings, and use either print or println where appropriate:
    package com.company;
    public class Main {
        public static void main(String[] args) {
            System.out.print("If you're happy and you know it ");
            System.out.println("clap your hands. ");
            System.out.print("If you're happy and you know it ");
            System.out.println("clap your hands. ");
            System.out.print("If you're happy and you know it ");
            System.out.println("and you really want to show it, ");
            System.out.print("If you're happy and you know it ");
            System.out.println("clap your hands. ");
        }
    }
  3. Run the code and check the song appears correctly in the output window
  4. Create a variable to store the string "clap your hands. " and name it action
  5. Modify the code so every time you previously had a string literal, you now use the variable.
  6. Run the code and check the song appears correctly in the output window
  7. Change the string assigned to action to "stomp your feet "
  8. Run the code and check the song appears correctly in the output window with the modified action
  9. Add another variable for the 'if you're happy and your know it sentence'. This time, you should choose how you name it (bearing in mind how variables should be named)
  10. Run the code again and check that it works as expected