wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion of primitives to Strings when printing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion of primitives to Strings when printing" Watch "Conversion of primitives to Strings when printing" New topic
Author

Conversion of primitives to Strings when printing

Theresa Fitzgerald
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Hello, this is my first post. I'm studying for the SCJP and was taking one of the mock exams today from the K & B book. I came across what I thought was an error in one of the questions, but I just tried to compile & run this code myself and found the answer was correct....but I don't understand why. Here is the question & answer (paraphrased a bit):
Given the following code, what will be output?

The correct answer is:
77
I'm confused because I thought that the + operator was overloaded to concatenate String objects together if any of the things being '+'d together was a string. So, why is the addition taking place in the first System.out.print, when the last item being '+'d together is a String? I thought that the toString would be called on the primitives instead of the addition taking place. Thanks for any help!
Edited by Corey McGlone: Added CODE Tags
[ March 09, 2004: Message edited by: Corey McGlone ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
toString is only called on the one argument to out(). So, the JVM first has to evaluate the expression inside, from left to right since they are all the same operator.
It starts by doing the addition of the array elements, and then finally, concatenates that value.toString() and the empty String. The return value from that of course, is a String, so that string is just output (in short, the string value of the sum of those elements).
If the empty string had been first, it would have been a whole different story, with the value "124" being output.
Hope this helps. Just remember order of operations, and left to right.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
Hi Theresa,
Please note that + operator can work as Unary, arthemetic or Concatenation operator.
How it behaves like that is depends on the operands. As you mentioned it behaves like a concat operator if one of the operators is String.
That itself answers your question. If you consider the question that you have posted,
first 2 '+' operators were between primitives so it will work as arthemetics operator. Only the thirs '+' works like a String concact operator.
Otherway round if the stament was
System.out.println(""+a1[0]+a1[1]+a1[2]) then you would have got '124'.
Hope this helps.


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
Theresa Fitzgerald
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Thank you both! I get it now.
Latha Kota
Ranch Hand

Joined: Mar 13, 2003
Posts: 35
Can anybody explain me what is happenning in the method adjust. I am not able to understand how the print statement is printing 7. Please help.
Latha
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The adjust method is modifying the int array from {1,2,3} to {1,2,4}. This is accomplished becuase objects are passed by reference in Java. If you're unclear on how variables are passed in Java, I recommend you check out this link.
Corey


SCJP Tipline, etc.
Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 235
One question.
Adjust returned a reference for a1.
Then, after call adjust, if i chance a2, will i chance a1 too ?


SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Vinicius Boson:
Adjust returned a reference for a1.
Then, after call adjust, if i chance a2, will i chance a1 too ?

Well, you're not doing anything to a1 or a2 - they're just references. However, after you invoke adjust, a1 and a2 reference the same object. Therefore if you modify the object a1 references, you can witness the change through a2, and vice versa.
 
jQuery in Action, 2nd edition
 
subject: Conversion of primitives to Strings when printing