• Post Reply Bookmark Topic Watch Topic
  • New Topic

logical & operator  RSS feed

 
Kellie Thompson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed when running the following code that it does not throw an exception when str is null.  I thought the logical & rule at line 9 would through an exception before it reaches  the System.out statement?


       
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kellie Thompson wrote:I noticed when running the following code that it does not throw an exception when str is null.  I thought the logical & rule at line 9 would through an exception before it reaches  the System.out statement?


First, that is not a logical & operator at that line. That is the bitwise & operator.

And second, what is the exception that is supposed to be thrown? ... meaning what is this rule you are referring to?

Henry


PS... welcome to the ranch.
 
Kellie Thompson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Got it.  The logical & is when you are using a boolean.  

If str is null, wouldn't a NullPointerException be thrown since str is null? 
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you won't get a NPE just because you test if something is null. NPEs are thrown when you try to call a method on a reference that is null, that is a reference that does not point to a valid object in memory.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kellie Thompson wrote:
Got it.  The logical & is when you are using a boolean.  


No. The logical AND operator is the && operator (two & characters). The single & operator is the bitwise AND operator.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kellie Thompson wrote:
If str is null, wouldn't a NullPointerException be thrown since str is null? 


To elaborate more on Junilu response. It only generates an NPE, if you dereference the null reference to access an instance variable, or call an instance method. Your example does neither of that.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& is the logical AND operator

& is the bitwise AND operator

You use && in boolean expressions

You can still use the & operator with boolean expressions. The difference with & when used with boolean expressions is that it doesn't short-circuit, unlike && which will short-circuit.  Read more about short-circuiting of conditional expressions here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
No. The logical AND operator is the && operator (two & characters). The single & operator is the bitwise AND operator.

Henry


Small note regarding logical AND operator:

In the JLS the '&' operator (for booleans) is also named logical operator while the '&&' is listed as conditional AND operator (should be around 15-22 if I'm not mistaken).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:
Small note regarding logical AND operator:

In the JLS the '&' operator (for booleans) is also named logical operator while the '&&' is listed as conditional AND operator (should be around 15-22 if I'm not mistaken).


Wow. Interesting. I did not know that.... and I stand corrected. Have a cow (actually, have two ).

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this page does list the & operator as the "bitwise AND"

So, & works as a logical AND when used with booleans but it works as a bitwise AND when used with numeric values.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Well, this page does list the & operator as the "bitwise AND"  . . ..
I always ignore that link because the names given by the Java™ Tutorials differ from those in the Java® Language Specification. &^| = bitwise AND XOR and (inclusive) OR on integers, and logical AND XOR and OR on [Bb]ooleans. && || = conditonal AND and OR, [Bb]ooleans only. This Java™ Tutorials page calls && || conditional and the first link calls them logical. I am sure the JLS wording is correct. And the JLS is the definitive authority in case of dispute.

Nobody has noticed the use of != true
Never use == true ==  false != true or != false. Not only are those expressions poor style, but they are completely unnecessary and error‑prone.
Not b == true or b != false, but plain simple b.
Not b == false or b != true, but !b.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!