Last week in high school my IT teacher gave us a programm, where one part she wrote interested me a lot.
In an if-statement, she checked if an element of an array was not null, so the programm could proceed with this element, therefore she wrote:
if (!(array[i] == null)
, which confused me, because with this method you are just using a bracket more, which makes it less easy to read. I changed it to ìf (array[i] != null)
and it worked just fine. But now I am confused, because as a teacher I assume you know a lot of your subject, so could there be a good reason why she used her method over mine? Is there some detail I don't know about?
In the vast majority of cases, a != b and !( a == b) will have exactly the same behavior, and a != b is almost always clearer.
A!= B means " A is not equal to B ". A=! B means "Assign the complement of B to A , and yield the lvalue of A ".
In Java, A == B and B == A always have the same semantics. In C# (which has operator overloading), there can be a difference if, say, B is an instance of a subclass of the class of A .
Answer1: a=b is used for assigning the values (rather then comparison) and a==b is for comparison. Answer4: Equals method compares both type and value of the variable, while == compares value.
There is no difference in the semantics of the two expressions. I would say there is no good reason to ever write the former.
But now I am confused, because as a teacher I assume you know a lot of your subject, so could there be a good reason why she used her method over mine?
The best thing to do is to ask your teacher. The most important thing to remember about teachers - as human beings - is that they have experiences, prejudices and are fallible.
In other words, she might have been bitten by some problem in the past which was solved by writing it like this - ask her about it, you might learn something.
Or, she perhaps thinks it's better, and can't articulate a strong benefit over personal preference; or that was the way she learnt, and hasn't seen a strong need to change - you learn something from that, insofar as there is more than one way to articulate the same semantics.
(I have a strong preference for !=
because it's neater - fewer parentheses; and why would the language designers bother providing !=
if you weren't intended to use it - but these are my personal preferences).
Or, she maybe meant to use !=
, and forgot to go back to fix it. It's easy to forget to clean things up.
In Java, there is no difference between a!=b
and !(a==b)
. Choosing the later form is a stylistic choice, which, to be honest, most static analysis tools/IDEs will issue a warning about.
Your teacher is using other ways to write the if-statement.
It doesn't matter if you write
if(array[i] != null)
or
if(!(array[i] == null))
because !
defines as not in most programming language. In this situation both of them are equal.
In conclusion, you can choose whatever style you like.
Hope you understood and good luck!
Always ask your teacher when you wonder about something :)
The other answers are correct. For your example there is no difference - chose what you like the most. But I'd like to give some more reasons to chose one way over the others.
My advice would be not to go for if (! (...))
in general because:
!(A || B || C || D)
vs !A && !B && !C && !D
. It might happen that in the former A-D have to be evaluated and then the resulting boolean is flipped wheras in the latter it might be enough to see that !A
evaluates to false and you can already stop there (because any conjunction with a false
member will be false
).NOT(is_a_digit OR is_a_letter OR is_a_dot)
instead of
NOT(is_a_digit) AND NOT(is_a_letter) AND NOT(is_a_dot)
.String someString;
that might contain a string or might be null
. Now imagine you'd like to check if its equal to "some specific string"
: someString.equals("some specific string")
- yikes, this might produce a NPE because when someString
is null there is no method equals
to call on it. Instead you'd have to do someString != null && someString.equals("...")
or the Yoda-way of "some specific string".equals(someString)
. There's also other helpers like Objects.equals()
.Sometimes extra parenthesis can make things easier to grasp and sometimes they don't.
While I don't agree that "there are no stupid question" I'd say that sometimes it's better to ask a dumb question than falling behind in the lessons because something nags at you or you missed some important bit. In this case I'd consider this a good question (unless she just told you why).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With