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.

Results 1 to 8 of 8

Thread: how to check null

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to check null

    Hi,

    Is there any other way to check null in string,without using == operator


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,254
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: how to check null

    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!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to check null

    some times == can't work(like string objects) that's why i wanted to know any other way...

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: how to check null

    @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 ==

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,254
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: how to check null

    Quote Originally Posted by veens View Post
    some times == can't work(like string objects) that's why i wanted to know any other way...
    Using == for null is fine, since you're comparing the reference (as Mr.777 already pointed out). If your reference is null, it will be == to null.
    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!

  6. #6
    Member
    Join Date
    Nov 2011
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: how to check null

    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 06:12 PM.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,254
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: how to check null

    Quote Originally Posted by mr.miku View Post
    Hi,
    Only when the left side value may be null then only == show exception so just try to avoid such on your code.
    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!

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: how to check null

    my 0.02
    String str = null;
    System.out.println(str.equals("hello world")); // NullPointerException
    System.out.println("hello world".equals(str)); // outputs false
    Improving the world one idiot at a time!

Similar Threads

  1. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  2. Check this code out
    By jwill22 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 11th, 2010, 08:34 PM
  3. how to check the value
    By javaking in forum Java Servlet
    Replies: 2
    Last Post: July 22nd, 2010, 06:56 AM
  4. Can Anyone Check This Link
    By arpitgadle in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2009, 08:56 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM