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");
Total: 34
Total: 7
7 is the total- In the first line,
"Total: " + ais text,Total: 3, and addingbto text joins it:Total: 34. - In the second line the parentheses make
a + ba 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);
-2147483648The numbers in this course stay far from that limit, but it's worth knowing that it exists.