Java program to print Integer between strings in java

Try to find 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 often – Initially the integers are added and we get the L.H.S. as 50. But, as soon as a string is encountered it's appended and that we get “50=” . Now the integers after ‘=’ also are considered as a string then are appended.

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

Output:

50=50

 

Submit Your Programming Assignment Details