Adding integer variables in Java
Go to solution
Solved by 79wjd,
2 minutes ago, DrMacintosh said:I'm literally new at this. Could you show me how the addition operator is set up?
System.out.println("The sum of 1 and 2 is " + (1+2));
Java's string builder will treat integers before a string like integers (and do any math on them if necessary). Any integers after the string will be treated as strings (unless you wrap them in parenthesis so that the addition is processed first).
E.g.
System.out.println("1+2"+7+7) --> "1+277"
System.out.println(7+7+"1+2") --> "1412"
System.out.println("1+2"+(7+7)) -> "1+214"
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now