Hey all,
This is my first post on my blog and i have something interesting stuff for newbies.
Code:
//Case 1
System.out.println(2 + 2 + ” Output “);
//Case 2
System.out.println(” Output ” + 2 + 2);
//Case 1
Console.WriteLine(2 + 2 + ” Output “);
//Case 2
Console.WriteLine(” Output ” + 2 + 2);
Any guess for the output ? Here it is:
Case 1 : 4 Output
Case 2 : Output 22
Why ? Its “Left To Right” Principle. Java and C# both uses it. I don’t know about other languages.
In the first case, It sums two integers : 2+2=4 and then converts it to a string: 4 Output
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: Output 22
Stay tuned!
