0

I understand this part:

    Integer myInt;
    myInt = new Integer(42);
    if (myInt != null && myInt.intValue() == 42) {
        System.out.println("Comparing 42 to 42");
    }
    myInt = null;
    if (myInt != null & myInt.intValue() == 42) {
        System.out.println("Comparing null to 42");
    }

Output:

Comparing 42 to 42
Exception in thread "main" java.lang.NullPointerException 

What I dont understand is this:

System.out.println("Result:" +(1 & 3));
System.out.println("Result2:" +(2 | 3));
Result:1
Result2:3

If is not comparing booleans why give this results?

Goldbones
  • 1,407
  • 3
  • 21
  • 55
  • 4
    Bitwise and **and** bitwise or. – Elliott Frisch Sep 19 '18 at 22:11
  • 3
    See https://stackoverflow.com/questions/11597978/are-the-bitwise-operators-or-logical-operators – dnault Sep 19 '18 at 22:11
  • @dnault, clearly dont respond to my question – Goldbones Sep 19 '18 at 22:12
  • 2
    I feel like @dnault linked to an excellent dupe which does explain the behavior you're seeing fairly concisely. – Makoto Sep 19 '18 at 22:13
  • @dnault, no problem. I understand now. Read this article https://www.programiz.com/c-programming/bitwise-operators – Goldbones Sep 19 '18 at 22:21
  • @Makoto, nowadays, seems that mark as duplicate is easier that respond to question. It was great If I saw an answer saying bitwise and then a calculation. – Goldbones Sep 19 '18 at 22:23
  • It'd be great if you assumed that no one meant you any offense in closing the question as a duplicate, and you followed the duplicate link to answers which would actually answer your question. – Makoto Sep 19 '18 at 22:24
  • If the link is not explicity assuming any answer why I even assumed that? The link has a question which answer is on oracle docs. – Goldbones Sep 19 '18 at 22:30
  • "If is not comparing booleans why give this results?" My question was very clear. "Your question seemed focused on why..." that´s an opinion. If my question is unclear I can edit. Marked as duplicate is a lazy answer and must be discouranged – Goldbones Sep 20 '18 at 02:10

0 Answers0