File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Strings doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings doubt" Watch "Strings doubt" New topic
Author

Strings doubt

Arnav Velimala
Ranch Hand

Joined: Jun 04, 2007
Posts: 37
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

Joined: Oct 30, 2001
Posts: 1970
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2718
    
    6

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.


SCJP, SCWCD.
|Asking Good Questions|
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just for fun, did you try (3+2) in parens? See if the result makes sense.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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

Joined: Jun 06, 2007
Posts: 2718
    
    6

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

Joined: Oct 02, 2003
Posts: 11497
    
  16

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"


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings doubt