aspose file tools*
The moose likes Beginning Java and the fly likes I need help,a question about String objects! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need help,a question about String objects!" Watch "I need help,a question about String objects!" New topic
Author

I need help,a question about String objects!

andy wang
Greenhorn

Joined: Apr 30, 2002
Posts: 17

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 ]

Name:Andy Wang<br />From:SouthEast University China.<br />Major:Communication Engineering
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
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 ]

doco
andy wang
Greenhorn

Joined: Apr 30, 2002
Posts: 17
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

Joined: Jan 31, 2003
Posts: 401
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

Joined: Jan 30, 2002
Posts: 3451
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.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
I would think a hearty thanks from both of us Michael; you silver toungued devil.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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

Joined: Apr 30, 2002
Posts: 17
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 !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need help,a question about String objects!
 
Similar Threads
someone please validate
why does one work and not the other?
intersting question
where is it possible that garbage collector...
I need help,a question about String objects!