String Variables
Strings are used for storing text.
A String variable contains a collection of characters surrounded by double quotes:
To set up a string variable, you type the word String followed by a name for your variable. Note that there's an uppercase "S" for String. Again, a semicolon ends the line:
String first_name;
Assign a value to your new string variable by typing an equals sign. After the equals sign the text you want to store goes between two sets of double quotes:
first_name = "Tony";
If you prefer, you can have all that on one line:
String first_name = "Tony";
Set up a second string variable to hold a surname/family name:
String family_name = "Stark";
To print both names, add the following println( ):
System.out.println( first_name + " " + family_name );
In between the round brackets of println, we have this:
first_name + " " + family_name
Try this exercise:
Run the programme. Your out put like this:


No comments