Core Java: String concatenation with + evaluates left to right

+ is left-associative, so numeric operands keep adding numerically until a String appears, after which everything to the right concatenates as text; the same numbers print differently depending on where the string sits.

Code
System.out.println(1 + 2 + "=" + 1 + 2);
System.out.println("=" + 1 + 2);
Output
3=12
=12
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms