Task 3 of 15 · 0 solved · 15 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

1.5 Strings — mixing strings and numbers

Lesson

+ with strings and numbers

When one side of + is a String, + turns the number into text and joins them. When both sides are numbers, + adds. Java works left to right, which causes a classic trap.


Worked example

"Age: " + 5     // Age: 5   (String + number, joins as text)
5 + 3           // 8        (number + number, adds)
"" + 5 + 3      // 53       (left to right: "5" then "53")
5 + 3 + ""      // 8        (5 + 3 = 8 first, then + "")

▶ Try it: run this, then change something and run it again.



What does "Age: " + 5 print?


What does "" + 5 + 3 print?


What does 5 + 3 + "" print?


Type exactly what this prints:

System.out.println("Sum: " + (2 + 3));

True or false?


Task

Write the body of label that takes a String name and an int age and returns the text name is age — for example, label("Ada", 30) returns Ada is 30.