|
Programming and Assembly Language Please read this Topic's rules!! |
![]() |
| LinkBack | Thread Tools | Rate Thread |
| |||
Because the reference you have is a reference to a string object, not the string itself perhaps? For instance, if I did something like: String str1a="Geo"; String str1b="rge"; String str2="George"; String str3="George"; String str1 = str1a + str1b ; What would str1, str2 and str3 look like? As Java can re-use an existing string object, str2 and str3 are probably the same as they both reference the same one string object. However, str1 and str2 will be difference, as they'll be different string objects. That's my understanding at least.
__________________ Last edited by Áedán; 16th November, 2007 at 02:33 PM. |
| ||||
You're on the right track, Áedán. The == operator tests to see if the two objects being compared are the same. It is possible for two strings to have the same lexical value but point to different objects, in which case the == operator will fail, even though the two strings are identical. |
| ||||
Actually, no. The assignment of 1a+1b creates an entirely new string. While str2 and str3 both point to the same object, str1 points to an entirely new object made by pasting str1a and str1b together. The interpreter has no way of knowing (at least, not an efficient way) that str1 is now equivalent to str2 and str3 in value, so the comparison fails. Last edited by Gizmo; 16th November, 2007 at 04:58 PM. |
| ||||
Use string.equals(comparedobject) or string.compareTo(tocomparestring) Those are the ones that'll compare both strings lexographically. String (Java 2 Platform SE 5.0)) Override the compareTo method in an Object if you want to test for equality in the Objects with defining String attributes.
__________________ Asus Maximus Formula (X38) Intel Q6600 3720Mhz TT Mozart Tx 2x WD Raptors 74gb R0 2x Maxtor 300gb Asus Geforce EN8800GT Windoors XP Pro sp2 Download here OcBible 1.55 & Guidemania v1.21 |
| ||||
Quote:
Thanks for your help, guys. Expect more java-related questions here soon! =D
__________________ ASUS X58 P6T-SE OCZ Gold 6GB DDR3-1600 750W Corsair CMPSU-750TX Intel Core i7 920 (Bloomfield) o/ced 4.03Ghz ATI Sapphire Radeon 4850 w/ Accelero Twin Turbo Cooler All housed in a Thermaltake Tai Chi Case with lots of silent fans. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Rate This Thread | |
| |
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sun GPLs Java; maybe Solaris? | Gizmo | OS, Software, Firmware, and BIOS | 2 | 18th November, 2006 03:37 AM |
Why Java? | dsio | Programming and Assembly Language | 25 | 23rd July, 2005 08:45 AM |
Java | Phat Pat | Betty's Rants and Rages | 2 | 30th March, 2003 08:32 AM |
Im taking JAVA!!! yeah! | The Spyder | Programming and Assembly Language | 30 | 9th June, 2002 10:52 PM |
Flash/Java | ItHuNkPuNk | Random Nonsense! | 1 | 10th March, 2002 02:45 AM |