• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion of primitives to Strings when printing

 
Theresa Fitzgerald
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Theresa Fitzgerald
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both! I get it now.
 
Latha Kota
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question.
Adjust returned a reference for a1.
Then, after call adjust, if i chance a2, will i chance a1 too ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic