0

I'm trying to figure out why & operator throws an exception and suspends my program but when I use && operator the program works with no errors.
Here is the portion of the code:

  • books is an arraylist of objects Book.
  • areYouBook returns true if the book's ISBN I'm looking for matches a.
  • The method findBook checks if I already have the book in the arraylist. When the method returns null, it means I have to create the book and add it to the ArrayList.

Thank you very much for your help!

public Book findBook(int a){
    int i=0;
    while((i<(books.size()))&!((books.get(i)).areYouBook(a)))
      i++;
    if(i<(books.size())) 
      return books.get(i);
    else
      return null;
}
moondaisy
  • 4,303
  • 6
  • 41
  • 70
Ailin W.B.
  • 21
  • 2
  • 1
    Because `&` is the [bitwise AND operator](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html), not the logical AND. – domsson Apr 16 '17 at 15:41
  • 2
    Also, I highly recommend you use braces `{}` for your blocks (`while`, `if`, `else`) all the time and indent your code properly. This is not only hard to read and understand, it is bound to execute in ways that you did not intend. – domsson Apr 16 '17 at 15:43
  • 1
    [Difference between & and &&](http://stackoverflow.com/questions/5564410/difference-between-and) – matoni Apr 16 '17 at 15:45
  • 1
    @domdom not for boolean operands. `&` *is* the [logical and operator](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.22.2). `&&` is the conditional and operator. – Andy Turner Apr 16 '17 at 17:10
  • 2
    @AndyTurner I really didn't expect to learn something that fundamental today (especially because I thought I knew all about this). I feel a bit embarrassed and quite enlightened. Thanks! – domsson Apr 16 '17 at 17:27
  • 1
    @domdom watch out, you're swimming in pedant-infested waters ;) – Andy Turner Apr 16 '17 at 17:28
  • "... `&` operator throws an exception". Well, that was specific. Not. Don't you think it might have been useful to tell us _which_ exception up front, including its exact copy-pasted text, if not (portions of) the stack trace? What was your reasoning in support of omitting such key information? – Lew Bloch Apr 16 '17 at 18:19
  • @Lew Bloch well, all the others could help me without that information, so the problem here is your understanding and, of course, your pedantry. Thanks anyway – Ailin W.B. Apr 16 '17 at 21:45

3 Answers3

1

I can tell you from here that the difference between the & operator and the && operator is that && will check the first expresion, and if it evaluates to false, then it will simply break from the check, but the & operator will run the following expression regardless of whether or not the first one turned out to be false.

So, the code you have written will check (i<(books.size())), and whether or not that turns out to be false, !((books.get(i)).areYouBook(a)) will still be executed.

If however, you used && and (i<(books.size())) was false, the second part of your check, which is !((books.get(i)).areYouBook(a)), will not be executed.

I hope this helps.

Edit:

You mentioned an error being thrown when you used the & operator instead of the && operator. Since the & operator does run the second expression even if the first one is false, I'm wondering if your second expression is actually throwing an error, and is getting called when you use &.

Edit 2:

Ok. Lets say that books has a size of 12 (indexes 0-11) just for demonstration. So, when you use the & operator, your while loop runs until i is 12. When i is 12, the loop executes (i<(books.size())) (which returns false) and CONTINUES to execute !((books.get(i)).areYouBook(a)). Inside the second expression, books.get(i) is being called, when i is 12. The books list only goes from indexes 0-11, throwing a java.lang.IndexOutOfBoundsException.

Kröw
  • 504
  • 2
  • 13
  • 31
  • Thanks, but when I use && the program works perfectly, but if I use &, I get this error message "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source)" – Ailin W.B. Apr 16 '17 at 15:56
  • @AilinW.B. Ahh, that makes sense. Let me edit my answer; I see what's going on... – Kröw Apr 16 '17 at 15:59
  • The error exception is in index 0, which I think it is because the arraylist is empty, right? – Ailin W.B. Apr 16 '17 at 17:11
  • @AilinW.B. Yes. In the error message, "Index" tells you the number that was passed into `get()` and "Size" tells you the size of the array, which is zero. – Kröw Apr 16 '17 at 17:14
0

java conditional operators says:

the operators && and || exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

the logical AND operator && will be short-cuircuting if the left side condition is false, on the other hand, when the left side condition is false the right side condition is never be executed.

but the bitwise & operator performs a bitwise AND operation.

Community
  • 1
  • 1
holi-java
  • 29,655
  • 7
  • 72
  • 83
-1

The logical AND that you're trying to use is represented by the && symbol i.e. True && False resolves to False. The single ampersand "&" represents a bitwise AND i.e. 0101 & 0100 resolves to 0110.

  • The single ampersand can also be used in place of the double ampersand with a slight change; the single ampersand will always execute the following expression whether or not the first one turns out to be true. – Kröw Apr 16 '17 at 15:51
  • 2
    Interesting, I've never seen the single ampersand used that way. Thanks for pointing that out! – Rob Misasi Apr 16 '17 at 15:59