Exercise 2 of 10 · Variables
Name Tag
What you will make
A bordered name-tag card with your own name across three of its lines, every one of them fed by a single line of code.
The one new idea: A variable in Java has a fixed type, and String stores text under a reusable name
Real programs mention the same value again and again — a name, a price, a filename. Storing it under one name means you fix it in one place instead of hunting through every line, and Java checking the type of that place before the program runs is a habit worth building early.
Go straight to the code ↓What a variable is in Java
Typing the same piece of text over and over is where mistakes come from. A variable lets you write it once and refer back to it — and in Java, it also lets the compiler know exactly what kind of value it is allowed to hold.
This line makes one:
String name = "Priya";Three parts, each doing its own job. String is the type — a promise to javac that whatever name holds
from now on will be text, never a number or anything else. name is the label. The equals sign is not asking
whether two things are equal; it is an instruction to take the value on the right and file it under the label
on the left. From then on, writing name anywhere in this method hands that value straight back to you.
Think of a labelled drawer with the label glued on before anything goes inside it. You put something in once.
Anyone who opens the drawer later gets whatever is sitting in it at that moment — and because the label also
says what kind of thing belongs in this drawer, trying to post a number into a drawer labelled String is a
mistake javac catches before the program ever runs, not one you discover halfway through.
One detail decides everything on the three lines below it: the quotes.
System.out.println("name")prints the four letters n, a, m, e. Quotes mean this exact text.System.out.println(name)prints what is stored under that label. No quotes means go and look this up.
A worked example
A different program, so the answer to this exercise is not given away:
public class Main {
public static void main(String[] args) {
String city = "Pune";
System.out.println("Weather report");
System.out.println("City: " + city);
System.out.println("Have a good day in " + city);
}
}Running it prints:
Weather report
City: Pune
Have a good day in PuneThe value "Pune" is written once, at the top, and used on two later lines. Change that first line to
String city = "Nagpur"; and both of those lines say Nagpur instead. The println lines themselves never
need editing — they only ever ask for whatever city is holding, joined onto the fixed text around it with a
plus sign.
That plus sign is doing two different jobs depending on what sits either side of it. Between two pieces of text it joins them end to end. You will meet its other job, adding numbers, in the next exercise.
Your turn
The editor already prints a name-tag card: a border, a header, a greeting, an owner line and a signature. Run it first, before changing anything, and you will see the card appear with the placeholder in it.
Now find the short line near the top:
String name = "YOUR NAME";Replace YOUR NAME with your own name, keeping the quotes, and press Run again.
Three lines of the card change at once, because all three ask the same variable for its value. You edited one line and the card followed.
Try it a second time with a nickname. That is the whole idea of a variable, and it is worth feeling rather than just reading.
If something goes wrong
The most common slip is quoting the label by accident. If a println line reads
System.out.println(" Welcome " + "name");, your card greets someone called name, because quotes turn it
into ordinary text rather than a lookup — and javac has no complaint, because that is a perfectly legal line.
The other one is dropping the quotes off your value, as in String name = Priya;. javac then treats Priya
as the name of something else entirely, goes looking for it, finds nothing, and reports cannot find symbol.
That message always means the same thing: you asked for a name javac has never been given a value for.
Neither is damage. Read the line the message points at, put the quotes back where they belong, and run it again.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Putting quotes around name inside one of the println lines
- Quotes mean plain text, so " Welcome " + "name" prints the four letters n-a-m-e instead of the value stored in the variable. Nothing stops this from compiling — a piece of text that happens to say name is a completely legal thing to want — so the only warning is the word appearing on screen where your name should be.
- Taking the quotes off the value
- Written as String name = Priya;, javac reads Priya as the name of something already defined — a variable, a class — finds nothing by that name, and stops with cannot find symbol before a single line runs.
- Leaving the type off the declaration, writing name = "Priya";
- Java variables need a type the first time they are created, so a line missing String reports cannot find symbol for name itself — javac has never heard of it, because as far as it knows nothing has introduced that name yet.
- Spelling the variable differently in one place, such as Name instead of name
- Java matches names letter for letter, including capitals, so Name and name are two unrelated identifiers to the compiler. It stops on whichever line uses the one that was never declared, reporting cannot find symbol for it.
Want a blank editor instead? Open the Java playground.