Forums Register Login

Strings doubt

+Pie Number of slices to send: Send
what is the output?

Will it be "10JP32" only?! or does it depends on the compiler - the order on which the elements are pushed on stack for evaluation? Possibly "5JP37"?

Thnaks
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Welcome to JavaRanch, Arnav!

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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Amit,


think this is because of the presence of string int the expression, so all further operands are treated as string.



Yes. You are right. since it does the concatenation operator and anything with the String, you will get a String.

HtH.
+Pie Number of slices to send: Send
Just for fun, did you try (3+2) in parens? See if the result makes sense.
+Pie Number of slices to send: Send
How about this program?



And the output it produces as follows:


7+2 is : 9
7+2 in same SOP is : 72
(7+2) in braces is : 9



Hope this helps a little further added to the descriptions given above.
+Pie Number of slices to send: Send
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.

So how does the above code work?

Please guide..
+Pie Number of slices to send: Send
the "+" operator is overloaded. consider it an added bonus feature the language developers gave you for this situation. It's similar to how they let you create strings with the shortcut

String myString = "Fred"
+Pie Number of slices to send: Send
Hi,


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 ]
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1031 times.
Similar Threads
25 horses to win, place, show
Java code to print 3 * 3 matrix
Converting Html to PDF
NumberFormat help!
Removing from an array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:33:58.