Author
reinitializing a String variable
Dan Acuff
Ranch Hand
Joined: Jul 13, 2009
Posts: 62
Shouldn't the code I have written work so far as the cm_pageName getting reset to HOME or LIST ?
What am I doing wrong in resetting cm_pageName?
I am just getting blank values and I have tested the value of cm_pageName to be one of the 2 strings shown.
Thanks,
Dan
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Never use == for String comparison - use the equals method instead.
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Dan Acuff
Ranch Hand
Joined: Jul 13, 2009
Posts: 62
Thanks a lot~
Looks like this for the record.
Sita Marivada
Greenhorn
Joined: Apr 01, 2010
Posts: 14
== is used to compare the values of primitive typed variables and the memory location of Objects.
equals is used to compare the content of the two objects.
== is normally much faster than equals since it just checks the memory location of two objects.
Sita Putta
Dan Acuff
Ranch Hand
Joined: Jul 13, 2009
Posts: 62
Oh I see.. and since String is not a primitive variable type, the equals method is better.
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
Dan Acuff wrote: Oh I see.. and since String is not a primitive variable type, the equals method is better.
That's correct. And using == to compare Strings is a little more complicated than with most objects because of the String pool. See Strings Literally .
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Apr 01, 2010 18:03:16
0
On line 2 there is no reason to create a new string.
subject: reinitializing a String variable