Java Arena
Step 3 of 6

Numbers and text together

+ has two jobs: it adds numbers, and it joins text. Java decides which one each + does, working from left to right. As soon as one side is a String, the + joins, and the result is a String again.

int a = 3;
int b = 4;
System.out.println("Total: " + a + b);
System.out.println("Total: " + (a + b));
System.out.println(a + b + " is the total");
Output
Total: 34
Total: 7
7 is the total
  • In the first line, "Total: " + a is text, Total: 3, and adding b to text joins it: Total: 34.
  • In the second line the parentheses make a + b a calculation first.
  • In the third line the two numbers come first, so they're added before any text appears.

Numbers have limits

An int can hold values up to 2147483647. A calculation that goes past the limit doesn't stop with an error: it wraps around to the negative end of the range and counts on from there, which is called overflow. One past the limit is the most negative int:

int big = 2147483647;
big = big + 1;
System.out.println(big);
Output
-2147483648

The numbers in this course stay far from that limit, but it's worth knowing that it exists.

Your task

Challenge 1 of 3

This program should print Total: 12 for the input 5 and 7, but it prints Total: 57. Fix it.

Challenge 2

Read two whole numbers and print the whole calculation. For the input 3 and 4 the program prints:

3 + 4 = 7

Challenge 3

Same idea with multiplication. For the input 6 and 7 the program prints:

6 * 7 = 42