Text with String
18 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A String holds text, like a name, a city or a message. You write the text in double quotes.
What is a String?
A ticket app must store the passenger's name and city, but a char holds only one letter. A String holds text of any length, written in double quotes.
String name = "Ananya";String grade = "A";String empty = "";The quotes are not part of the text; they only show where it starts and ends. "" is an empty String: text with no characters at all.
String starts with a capital S because it is a class, not a primitive type (see Data types in Java (opens in a new tab)).
Join text with +
You want to print "Hi, Ananya from Pune", but the name and the city are in two variables. When at least one side of + is a String, + joins the parts into one String; this is string concatenation.
Example 1 · Join text with +
public class Greeting { public static void main(String[] args) { String firstName = "Ananya"; String city = "Pune"; char section = 'B'; String message = "Hi, " + firstName + " from " + city; System.out.println(message); System.out.println("Section " + section); }}Hi, Ananya from Pune Section B
What this code does
messagejoins four parts into oneString. Java adds no spaces for you, so they are inside the quotes:"Hi," + firstNamewould giveHi,Ananya.- The other side of
+can be a number, acharor aboolean."Section " + sectionjoins thechar, just as"Marks: " + 90givesMarks: 90.
Watch out: text first, then numbers
A bill has books for 450 and pens for 50. "Total: " + books + pens looks right, but it prints Total: 45050.
Java works from left to right. "Total: " + 450 is already text, so 50 is joined as text too. To add the numbers first, put them in brackets: "Total: " + (books + pens) gives Total: 500. Java always works out the brackets first. Step through the same rule with smaller numbers, then try your own in the lab.
"Sum: " + 5 + 3, left to right
Step 1 of 7
"Sum: " + 5.Experiment
Join or add?
Change the two prices. Watch where the brackets and the order change the answer.
- "Total: " + books + pens
- Total: 45050
- "Total: " + (books + pens)
- Total: 500
- books + pens + " rupees"
- 500 rupees
Ask a String: length() and toUpperCase()
A form allows names of up to 20 characters, and a ticket prints the name in capitals. A String comes with ready-made actions called methods: write the variable, a dot, then the method with its brackets.
Example 2 · Count characters and change case
public class Ticket { public static void main(String[] args) { String fullName = "Rahul Sharma"; String nickname = ""; System.out.println(fullName.toUpperCase()); System.out.println("Characters: " + fullName.length()); System.out.println("Nickname length: " + nickname.length()); System.out.println(fullName); }}RAHUL SHARMA Characters: 12 Nickname length: 0 Rahul Sharma
What this code does
fullName.length()gives 12, because the space counts too. An empty String has length 0.toUpperCase()gives a new text in capitals;fullNameitself is unchanged, as the last line shows.
The Strings module (opens in a new tab) covers many more methods.
Experiment
String lab
Type any text. See what each method gives.
- s.length()
- 6
- s.toUpperCase()
- ANANYA
- "Hi, " + s
- Hi, Ananya
Next step
Check your understanding
8 quick questions