“Left To Right” Principle

puneetkay 0 Tallied Votes 107 Views Share

This program consists of a function in one class. It demonstrates the "Left To Right" Principle used by most of the languages.

public class Demo {
	
	public void demonstration(){
		System.out.print("Case 1 : ");
		System.out.println(2 + 2 + "Output");
		
		System.out.print("Case 2 : ");
		System.out.println("Output" + 2 + 2);
	}
	public static void main(String[] args) {
		new Demo().demonstration();
	}
}

/*
Output :
==================
Case 1 : 4Output
Case 2 : Output22

Answer :
==================
In the first case, It sums two integers : 2+2=4 and then converts it to a string: 4Output
In the second case Java takes string ” Output ” then converts 2 to a string, concatenates them together. Then this process repeats with next number… Finally we get:  Output22

*/