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 5 of 5

Thread: What can go wrong if you replace && with & in the following code:

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post What can go wrong if you replace && with & in the following code:

    What can go wrong if you replace && with & in the following code:


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What can go wrong if you replace && with & in the following code:

    String a=null;
    if (a!=null && a.length()>10)
    {...}

    A single ampersand here would lead to a NullPointerException
    Last edited by helloworld922; February 11th, 2010 at 11:23 AM.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: What can go wrong if you replace && with & in the following code:

    && is a shortcut and it means that if the condition on the left is false it wont even bother looking at the condition on the right, if you use a single amp instead it will force it to evaluate both conditions even though it already knows that the whole condition will fail.

    And in the above post from Sharad you will see that if you replace the double amps with a single amp it will first check to see if a is not null which will evaluate to false since a is null and then it will check the right side anyways because of the single amp and when you do a.length() it blows up.

    // Json

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What can go wrong if you replace && with & in the following code:

    In addition, && only operates on booleans, while & does a full boolean and operation on any primitive data type.
    ex:
    int a = 0x10; // a = b00010000
    int b = 0x08; // b = b00001000
    System.out.println(a & b); // prints out 0x18, or b00011000, or 24d

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: What can go wrong if you replace && with & in the following code:

    Have a look at Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) for more information on Bitwise operators.

    // Json

Similar Threads

  1. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM
  2. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM
  3. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 01:03 AM
  4. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 07:34 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 09:49 AM

Tags for this Thread