| |||||||
| 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 08:33 AM. |
| ||||
| 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.
__________________ Avatar and sig graphic by Pitch. Subscribers! Ask about a custom graphic or avatar today! Gizmo Thermal Diode Mod and Direct-Die Water Block 8-Cheetah 18GiB U-2 SCSI MegaRAID Enterprise 1500/128MiB Samsung SyncMaster 955DF TTGI/Superflower TTS-520 PSU ![]() ![]() ![]() |
| ||||
| 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.
__________________ Avatar and sig graphic by Pitch. Subscribers! Ask about a custom graphic or avatar today! Gizmo Thermal Diode Mod and Direct-Die Water Block 8-Cheetah 18GiB U-2 SCSI MegaRAID Enterprise 1500/128MiB Samsung SyncMaster 955DF TTGI/Superflower TTS-520 PSU ![]() ![]() ![]() Last edited by Gizmo; 16th November, 2007 at 10:58 AM. |
| ||||
| 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
__________________ ![]() ![]() Total hard disk space: 3.5TB ![]() |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Rate This Thread | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sun GPLs Java; maybe Solaris? | Gizmo | OS, Software, Firmware, and BIOS | 2 | 17th November, 2006 09:37 PM |
| Why Java? | dsio | Programming and Assembly Language | 25 | 23rd July, 2005 02:45 AM |
| Java | Phat Pat | Betty's Rants and Rages | 2 | 30th March, 2003 02:32 AM |
| Im taking JAVA!!! yeah! | The Spyder | Programming and Assembly Language | 30 | 9th June, 2002 04:52 PM |
| Flash/Java | ItHuNkPuNk | Random Nonsense! | 1 | 9th March, 2002 08:45 PM |