AP java - De Morgan's Laws

The famous De Morgan’s laws was developed by Augustus De Morgan in the 1800s, he was a indian born mathmatician and logician. And heres how the law applies in java

De Morgan’s Laws

Distrubuting the ! (NOT) operator

TL;DR

1
2
3
4
5
6
7
8
&& becomes ||
|| becomes &&
< becomes >=
> becomes <=
<= becomes >
>= becomes <
== becomes !=
!= becomes ==

AND & OR operators


Here’s an example:

1
!(A && B)

According to De Morgan’s law, it’s the same as the following.

1
!A || !B

Remember that you’ll have to change it from OR to AND to make this true

Bigger than & smaller than


1
!(A > B)

According to De Morgan’s law, it’s the same as the following.

1
A <= B

Note that the ! operator made the sign inclusive, since > is exclusive.

Double equals


1
!(A == B)

this one should be pretty obvious and simple

1
A != b

the opposite of == would be !=

In the context of java

1
2
3
4
5
6
7
8
9
public class DeMorgan
{
public static void main(String[] args)
{
int x = 6;
int y = 9; //very nice haha so funny
System.out.println(!((x < 9) && y > 6));
}
}

Let’s take a look at the logic part

1
!((x < 9) && (y > 6))

Using De Morgan’s laws, it would be the same as the following

1
!(x < 9) || !(y > 6)

Now, apply the laws on them

1
x >= 9 || y <= 6

After that, plug in the values for each variables

1
2
3
int x = 6;
int y = 9; //very nice haha so funny
System.out.println(x >= 9 || y <= 6);

Since x is not bigger or equal to 9, or y is smaller or equal to 6, the answer should yield false

Let’s try running it

1
2
3
false

Process finished with exit code 0

EOF
icanc.space