This program consists of a function in one class. It demonstrates the "Left To Right" Principle used by most of the languages.
“Left To Right†Principle
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
*/
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.