Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reinitializing a String variable

 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == for String comparison - use the equals method instead.
 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot~

Looks like this for the record.

 
Sita Marivada
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== 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.
 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see.. and since String is not a primitive variable type, the equals method is better.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 2 there is no reason to create a new string.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic