Java String Concatenation Gotcha

Day 2 - A simple yet tricky aspect of Java Here's a small Java logic that is easy to overlook: System.out.println(5 + 5 + "5"); System.out.println("5" + 5 + 5); At first glance, both statements seem quite similar. However, the outputs are: 👉 105 👉 555 The key difference lies in the order of operations. In the first case, 5 + 5 results in 10, which then concatenates with "5" to produce 105. In the second case, since "5" appears first, everything that follows is treated as a string, resulting in 555. 👉 Once a String is involved, the operation becomes concatenation. This is a straightforward concept, but it's one of those details that can easily be missed if we're not paying attention. #Java #Programming #Coding #JavaDeveloper #Learning #KLU #CSE #ComputerScience

  • text

To view or add a comment, sign in

Explore content categories