Thursday, September 4, 2025
HomeLanguagesJavaPrinting Integer between Strings in Java

Printing Integer between Strings in Java

Try to figure out the output of this code:




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.




public class Test
{
    public static void main(String[] args)
    {
        System.out.println(45+5 + "=" +(45+5));
    }
}


Output:

50=50

This article is contributed by Himanshi Gupta. If you like Lazyroar and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS