This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I need help,a question about String objects!

 
andy wang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried this piece of code and got the output:
true
false

I have referd to the JLS,and it says:"The string concatenation operator +, which, when given a String operand and an integral operand, will convert the integral operand to a String representing its value in decimal form, and then produce a newly created String that is the concatenation of the two strings"
Then ,How can i explain the output?
[ May 08, 2003: Message edited by: andy wang ]
[ May 08, 2003: Message edited by: andy wang ]
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using s.equals( someString )

Outputs true, true

Equals() This compares an object passed as an argument with the current object, and returns true if they are the same object (not just equal - they must be one and the same object). Or returns false if they are different objects, even if the objects have identical values for their data members.
Page 239, Beginning Java2 1.3 Edition, Horton, Wrox Press.

Also see immutable objects or do a google search on immutable objects.
[ May 08, 2003: Message edited by: Donald R. Cossitt ]
 
andy wang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Donald R. Cossitt!
I know what you mean But i am wondering why the code "test"+010 and "tesst"+i generate different String objects.
Thank you all the same.
[ May 08, 2003: Message edited by: andy wang ]
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also searching out the "test" + 010 issue for myself. Can you tell me what the subject discussed in your example was? I am myself fairly green and am not familiar with this (or have read it and just don't remember ).
Essentially it boils down to immutability of objects. "test" + i is concatenated in the toString() method of the String object but yet two separate 'objects' so the comparison of "test8" and "test" + i will fail as "test8" == "test" + i. So now you have two compare them as 'objects' with equivalent value when doing the s.equals( "test" + i ). (s is obj1 and "test" + i is obj2)
[ May 08, 2003: Message edited by: Donald R. Cossitt ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then ,How can i explain the output?
Basically, the JVM pools String literals for efficiency. Any String literal that the compiler parses goes into that pool. It is also important to remember the difference between the == operator and the equals() method. The == operator, in regards to an object reference, returns true if the two compared references point to the same object on the heap. The equals() method for a String compares the contents of the two Strings. The first sysout prints true because the compiler has enough information to create a String literal from "test"+010, as you know if a literal int is begun with a 0 it is considered octal or base 8, so it evaluates to "test8". Now since we already have a String literal in the pool with those contents then the newly generated literal points to the same object and thus == returns true.
In the second case, since we are using a variable that might change, then as you mentioned, the compiler creates a StringBuffer object to concatenate the two values. This will create a new String object, not in the pool, so therefore the == fails and false is printed.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think a hearty thanks from both of us Michael; you silver toungued devil.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think a hearty thanks from both of us Michael; you silver toungued devil.
This is a subject that I (and I'm sure most) had a lot of trouble understanding when I first started playing around with Java. The explanation from Complete Java 2 Certification Study Guide by our own Michael Ernest et. al., really nailed it down for me. I didn't know about JavaRanch at the time, so that book was all I had to pass the Programmer Certification and it definitely serverd its purpose. I would recommend it to anyone before taking that test.
 
andy wang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In effect, i also started the same topic int the forum of Programmer Certification Study, and i got a very good explanation,see
I need help,a question about String objects!
Many thanks to Michael Morris !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic