Open In App

Printing Integer between Strings in Java

Last Updated : 11 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Try to figure out the output of this code: Java
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(45+5 + "=" +45+5);
    }
}
Output:
50=455

The reason behind this is - Initially the integers are added andĀ  we get the L.H.S. as 50. But, as soon as a string is encountered it is appended and we get "50=" . Now the integers after '=' are also considered as a stringĀ  and so are appended.

  • To make the output 50=50, we need to add a bracket around the sum statement to overload the concatenation operation.
  • This will enforce the sum to happen before string concatenation as bracket as the highest precedence.
Java
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(45+5 + "=" +(45+5));
    }
}
Output:
50=50


Next Article

Similar Reads