Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #477 > unrolled thread
| Started by | Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> |
|---|---|
| First post | 2011-04-02 22:26 +0200 |
| Last post | 2011-04-03 12:07 +0200 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.java.help
String comparisons Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-02 22:26 +0200
Re: String comparisons Aéris <aeris@imirhil.fr> - 2011-04-02 23:23 +0200
Re: String comparisons Aéris <aeris@imirhil.fr> - 2011-04-02 23:27 +0200
Re: String comparisons Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 18:59 +0200
Re: String comparisons Lew <noone@lewscanon.com> - 2011-04-06 12:19 -0400
Re: String comparisons Roedy Green <see_website@mindprod.com.invalid> - 2011-04-02 21:17 -0700
Re: String comparisons Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 12:07 +0200
| From | Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> |
|---|---|
| Date | 2011-04-02 22:26 +0200 |
| Subject | String comparisons |
| Message-ID | <87r59kpfgs.fsf@merciadriluca-station.MERCIADRILUCA> |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I'm given the following Java code snippet:
==
String d = new String("abcd");
String s = new String("abcd");
String c = d;
String n = c.clone(); // see discussion here after
==
Using System.out.println for the following args:
1. d == s
2. d == c
3. c == n
4. d.equals(s)
5. s.equals(n)
6. d.equals(null)
gives respectively
1. FALSE
2. TRUE
3. TRUE // supposed
4. TRUE
5. FALSE // supposed
6. FALSE
where supposed comments are there because my compiler does not accept
the
==
String n = c.clone();
==
instruction in the main program (because
clone() has protected access in java.lang.Object
).
First, I want to understand which values are compared in 1., 2. and 3.
Second, I want to understand what I should change to get no compiler
error using the clone method on c.
Thanks.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --
The ends justify the means.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iEYEARECAAYFAk2XhnMACgkQM0LLzLt8MhzCMQCeMIzts7Vp9nJ+1E8VzSuLOr0q
MvcAnj7onoXG7HySWm10tae2syRJgjwZ
=L0em
-----END PGP SIGNATURE-----
[toc] | [next] | [standalone]
| From | Aéris <aeris@imirhil.fr> |
|---|---|
| Date | 2011-04-02 23:23 +0200 |
| Message-ID | <4d9793b9$0$7284$426a74cc@news.free.fr> |
| In reply to | #477 |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Le 02/04/2011 22:26, Merciadri Luca a écrit : > 1. d == s > 2. d == c > 3. c == n > 4. d.equals(s) > 5. s.equals(n) > 6. d.equals(null) > > gives respectively > 1. FALSE > 2. TRUE > 3. TRUE // supposed > 4. TRUE > 5. FALSE // supposed > 6. FALSE This is totally wrong d, s and n are 3 different instances, so d == s and c == n are all FALSE. c is the same instance than d, so d == c is TRUE. But all represent the same string "abcd" so all .equals() all TRUE. For the compiler error, Object.clone() is protected so you can't use it until you override it. And String is immutable in Java, so there is no interest to clone String. If you want to copy String, use new String(c) instead, but I don't see the goal? - -- Aeris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJNl5OuAAoJEK8zQvxDY4P909gH/0lMDlFcGjiuYtUXXY3hYXY0 v2b9afm9dyfwvx9ErpvB43mYaapilyNeRM5MaEH2NDgmAAwJ0NmJCBuNWSsrmLan 3Xix6g5zgLklaZUql5tHXjQ1nHa35pU1NiWF7MZi0MTqyshKNr6hJquLTmwcO8pn 4yQ70WgsL7wyrL8uejdzDDCC8h3wVqZDZapjyj/rcFq8Tjo8C9CXcGABlvrgBt9n Y5hDfF7cfb3/rjfceLykuIfUehFEpsRgNuuMmEGKncW1y6tyZQt+SlW9T2mMO/uF p8XL09Dl+bxsVyhACeqMtPwt7Nz0Nj9y20uhk7lZ+Hcyy7cGRX6e5xrrgiDGYSE= =khfT -----END PGP SIGNATURE-----
[toc] | [prev] | [next] | [standalone]
| From | Aéris <aeris@imirhil.fr> |
|---|---|
| Date | 2011-04-02 23:27 +0200 |
| Message-ID | <4d9794ac$0$14989$426a74cc@news.free.fr> |
| In reply to | #480 |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Le 02/04/2011 23:23, Aéris a écrit : > But all represent the same string "abcd" so all .equals() all TRUE. Except for the d.equals(null), of course? - -- Aeris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJNl5SrAAoJEK8zQvxDY4P9l1UH/17yRm8oivQP0WbUWBsRZLft FpILLpi4ozPXGEGtDb6vWUeqVcCKzlur0kMC3t+g/L9vWIhDNPWPq/ZSwoI1cg9W hBYIKDcHUnTd8Ahp43z50Jer2UEauqBMgLNT22JnQrGlLceZukQuUh+3dB6Jfpi2 sMnovEZdJEyqA3bp9G25n1repF7nln3iAg09JWTFqTEM+6HffR6pJuTvPTMllXMe WbmAFxVtcTkC2w9ja4AbeCJ2LqfG7k+JameoYk9bs8KLght8t81AmL90Q6LhgucJ 0eYOuAksTg+mHjgDVAysHCGrM+7kXs+q6Gi5oKTGTNOmZVypC4iu14Kyhx9un6Q= =+dlX -----END PGP SIGNATURE-----
[toc] | [prev] | [next] | [standalone]
| From | Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> |
|---|---|
| Date | 2011-04-03 18:59 +0200 |
| Message-ID | <87zko72rv4.fsf@merciadriluca-station.MERCIADRILUCA> |
| In reply to | #480 |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I can explain why 1., 2., 4. and 6. are respectively FALSE, TRUE, TRUE and FALSE. Considering ints apart, the == operator returns TRUE e.g. in a == b if a and b are same objects. As a result, d != s is true, and d == s is false because d and s are not the same objects. For question 2., d == c is true because of the affectation String c = d. That is, d and c are exactly the same object. This was not what I had understood from reading Aéris' message at first sight, but re-looking at it, this is the same explanation. Thanks, Aéris. I would have better understood if one had said that == tests if the two objects are the same! - -- Merciadri Luca See http://www.student.montefiore.ulg.ac.be/~merciadri/ - -- If all else fails, try the obvious. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/> iEYEARECAAYFAk2Yp28ACgkQM0LLzLt8MhzIewCfS0BwgVjAbBsqAqZQD/NLnB2U r4cAn3+CUhvJFdhj08ITlKaG3wPw//WO =Kw4j -----END PGP SIGNATURE-----
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2011-04-06 12:19 -0400 |
| Message-ID | <ini3qj$4mc$3@news.albasani.net> |
| In reply to | #513 |
Merciadri Luca wrote: > I can explain why 1., 2., 4. and 6. are respectively FALSE, TRUE, > TRUE and FALSE. > > Considering ints apart, the == operator returns TRUE e.g. in a == b if > a and b are same objects. As a result, d != s is true, and d == s is > false because d and s are not the same objects. More precisely, they are not objects at all but pointers to objects. > For question 2., d == c is true because of the affectation String c = > d. That is, d and c are exactly the same object. Not true. They are pointers to the same object. > This was not what I had understood from reading Aéris' message at > first sight, but re-looking at it, this is the same > explanation. Thanks, Aéris. I would have better understood if one had > said that == tests if the two objects are the same! That's in the tutorials. You should read the tutorials. We've been recommending them for a long time. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-04-02 21:17 -0700 |
| Message-ID | <4qrfp694220avotb5l7n064ehrouh0v64r@4ax.com> |
| In reply to | #477 |
On Sat, 02 Apr 2011 22:26:27 +0200, Merciadri Luca
<Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
someone who said :
>String d = new String("abcd");
>String s = new String("abcd");
>String c = d;
>String n = c.clone(); // see discussion here after
new String almost never needed. Just use "abcd".
You can't use String.clone because it is a protected method and
String is final. So any speculation about its behaviour is
theological.
See http://mindprod.com/jgloss/gotchas.html
especially the one on string comparison.
See also http://mindprod.com/jgloss/string.html
http://mindprod.com/jgloss/interned.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
Doing what the user expects with respect to navigation is absurdly important for user satisfaction.
~ anonymous Google Android developer
[toc] | [prev] | [next] | [standalone]
| From | Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> |
|---|---|
| Date | 2011-04-03 12:07 +0200 |
| Message-ID | <87lizrbqbu.fsf@merciadriluca-station.MERCIADRILUCA> |
| In reply to | #477 |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Thanks for all the answers. Well, this is not `totally wrong.' What you meant is that the two values I supposed (i.e. 3. and 5.) are incorrect. That is, 3. should be FALSE, and 5. should be TRUE. Thanks for the explanations. - -- Merciadri Luca See http://www.student.montefiore.ulg.ac.be/~merciadri/ - -- First deserve, then desire. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/> iEYEARECAAYFAk2YRvUACgkQM0LLzLt8MhxBzQCgmBDiW1L29ltYIdVFC+i8UxjO //QAoJTHSK3zFjkOlHpra4aTBj2dAW2u =yjPi -----END PGP SIGNATURE-----
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web