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'
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.print("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.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);
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.");
}
}
action
action
to "stomp your feet"