Hi,
Is there any other way to check null in string,without using == operator
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Is there any other way to check null in string,without using == operator
Probably not. But what are you actually trying to do? Why do you think you should be avoiding the == operator in this case?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
some times == can't work(like string objects) that's why i wanted to know any other way...
@veens: There is no way that == doesn't work.
== is a comparison operator that compares the referenced value of object.
What you will be trying to do is comparing the contents, not the referenced value.
So, for comparing the contents you can use equals(), while for comparing referenced value you must use ==
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Hi,
Only when the left side value may be null then only == show exception so just try to avoid such on your code.
Core java
Last edited by mr.miku; January 11th, 2012 at 07:12 PM.
If I'm understanding you correctly, you're saying that this will cause an Exception:
String str = null; boolean n = (str == null);
...and that's simply not true. Using == to check null is the way to go.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!