|
Author |
Topic: String comparison (Read 243 times) |
|
ydur75 Guest
|
String comparison
« on: May 18th, 2004, 7:32pm » |
|
How I can compare a string variables with a string value. i.e. is correct do this?: String name="sam"; if (name=="sam") { } Thanks Rudy
|
|
|
|
Sprak
|
Re: String comparison
« Reply #1 on: May 18th, 2004, 8:58pm » |
|
Best way to find out is to try it You are correct however.
|
|
|
|
justo
|
Re: String comparison
« Reply #2 on: May 18th, 2004, 9:41pm » |
|
the == operator will only return true when comparing two object when they are the same object...ie (name == name) will always be true but (name == "sam") will always be false...because "sam" is implicitly another string object. also, Code:String name = "sam"; String name2 = name; if (name == name2) { //will be true because they point to the same object } |
| what you want is name.equals("sam")...it will be true when the strings are the same.
|
|
|
|
ydur75 Guest
|
Re: String comparison
« Reply #3 on: May 19th, 2004, 11:46am » |
|
Thank you very much justo. Rudy
|
|
|
|
|