When I studied java, I noticed that we avoid using ==
to compare reference types because ==
compares whether the references are the same, not the contents. And we would only use ==
for primitive types due to the way they're being stored in the memory.
I found a kinda similar note in R documentation for Relational Operators:
Do not use == and != for tests, such as in if expressions, where you must get a single TRUE or FALSE. Unless you are absolutely sure that nothing unusual can happen, you should use the identical function instead.
And immediately after this, I found:
For numerical and complex values, remember == and != do not allow for the finite representation of fractions, nor for rounding error. Using all.equal with identical is almost always preferable.
My humble questions:
1. Do we talk about primitive types in R? If so, what are they? And can we always safely use relational operators to compare them? (furthermore, under what situations could we be "absolutely sure that nothing unusual can happen" when using relational operators?)
2. I saw R codes comparing strings (characters) using ==
many times, are those R codes I saw just being sloppy or is that because character/string a primitive type in R (or something that we could always use relational operators to compare)?
[Updates]
Thanks for the comments below, I realized that the upper quote above is mainly trying to emphasize the vectorizing feature of R‘s operations rather than the accuracy of the output, and the validity of the relational operations in (base) R is highly unlikely to be affected by issues related to reference types.
Any answers or comments for further explanation/clarification are heartily welcomed.