• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings doubt

 
Arnav Velimala
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for fun, did you try (3+2) in parens? See if the result makes sense.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic