"I Think, I Conceptualize, I Analyze, I Design, and I Create." ~ Puneet Kalra

Cognitive Robotics Research Centre Of University Of Wales, Newport Puneet Kalra

Home Studies Research Projects Tutorials Portfolio

Puneet Kalra - www.puneetk.com - Socializing Robots

Left To Right Principle

June 25th, 2009 by Puneet Kalra Leave a reply »

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!

Leave a Reply

Please note : I will help you only if you will show some efforts! Don't expect replies to simple queries, You can easily find answers for them through searching.

I'm a student and I work as well. So i might reply late here. If you have something important to discuss Or stuck somewhere in your project. Please post on my Facebook page for a quick response.