Re: when using equals method with a null string...
- From: Tony Burrows <tony@xxxxxxxxxxxxxxx>
- Date: Fri, 10 Mar 2006 16:04:56 +0000
On Fri, 10 Mar 2006 07:22:03 -0800, minjie wrote:
If I have declared 2 strings as follows:I've just tried this code in Java 1.4 and 1.5
private String myStr1 = "";
private String myStr2 = "";
and somewhere in the application they may or may not get assigned
values. When I do
if (myStr1.equals(myStr2))
....
I will get an exception if myStr1 is null. How do I test it without
using
if (myStr1 != null)
if (myStr1.equals(myStr2))
....
?
I have a lot of string comparisons like the above, and if I have to
test one by one like that, it's a lot of code!
Thanks.
public class testCode{
public static void main (String[] args){
String s = "";
String y = new String("");
System.out.println("Tony equals string y"+ y.equals("Tony"));
System.out.println("Tony equals string s"+ s.equals("Tony"));
}
}
and it compiles and runs with no problems. I don't get a null pointer at
all. I can't get one with an if statement either.
Tony
.
- Follow-Ups:
- Re: when using equals method with a null string...
- From: njzy333
- Re: when using equals method with a null string...
- References:
- when using equals method with a null string...
- From: minjie
- when using equals method with a null string...
- Prev by Date: Re: Magic number in Boolean
- Next by Date: Re: Compiler telling me I have 1.4 JDK when i have 1.5 wont compile generics code
- Previous by thread: Re: when using equals method with a null string...
- Next by thread: Re: when using equals method with a null string...
- Index(es):
Relevant Pages
|