Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes reinitializing a String variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reinitializing a String variable" Watch "reinitializing a String variable" New topic
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: 19556
    
  16

Never use == for String comparison - use the equals method instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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

On line 2 there is no reason to create a new string.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reinitializing a String variable
 
Similar Threads
How do we spot what will cause a NullPointerException?
NullPointerException... what is left to check?
File Creation Problem in unix
java.Lang.classCastException
Arrays.asList()