I think it probably prints what you first said, though I am not sure and not interested enough to work it out.
What we can say for sure is that it will always produce the same result on every Java compiler and JVM. The language spec carefully defines operator precedence, expression evaluation etc.
Although the answer to the question will be fixed and well-defined by the spec, anyone writing code like that in a real application deserves to be ruthlessly mocked. Brackets should be used to make it clear. That applies to any complex expression. Maintainablity is important.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
How expressions are evaluated by a Java compiler or runtime environment is described within The Java Language Specification (also known as the JLS, for short). If you're not already familiar and comfortable with this document, as you learn Java, you may find that you soon will be (though it may initially appear to be somewhat intimidating, i.e. technical and boring).
Different implementations of Java compilers or runtime environments should all adhere to the rules in the JLS (otherwise, I suppose, they'd arguably not be Java compilers or runtime environments). (Note: The Java language has changed slightly since version 1.0, though I don't presently recall how any of these changes pertain to this conversation.)
The expression 7 + 2 + "JP" + 3 + 2 should be evaluated the same way by all Java compilers and runtime environments. The evaluation order is especially relevant. As you've likely already discovered, this example expression is evaluated from left to right (and will always evaluate to "10JP32").
Are the steps of the evaluation of this example expression clear? [ June 12, 2007: Message edited by: Dirk Schreckmann ]
Hi, the explanation provided by Dirk Schreckmann is the required answer.But I wonder why 3+2 is not evaluated to 5? I think this is because of the presence of string int the expression, so all further operands are treated as string. If I am wrong, please provide the correct information.
Hi, The discussion above concludes the fact that when string is present in the println method's expression, the remaining operands are converted to string. I just want to know how does this take place? Because, the operands in the println are/may be primitive datatypes, and as far as I know, there is no toString() method defined for primitive datatypes and Autoboxing is supported in Java 5.0 not in the previous versions.
Originally posted by Amit Ghorpade : The discussion above concludes the fact that when string is present in the println method's expression, the remaining operands are converted to string.
Yes you are right but with a small clarification. Its not always whenever a String is present in the SOP the remaining operands are converted into String! But the operands whichever follows one String, will all be treated as Strings.
If you look at the sample code i have given above, that will make it crystal clear. Again, just to make it clarified more,
As Fred Rosenberger said, the + operator in Java is overloaded!. Don't forget that.
Hope this helps. [ June 19, 2007: Message edited by: Raghavan Muthu ]