== Vs equals

meglio usare == a seguire il perche'

Differenza tra "==" e "equals"

  • la prima cosa da notare e' che "==" in Java e' un operatore mentre in Scala e' un methodo
  • "equals" controlla in contenuto , va in errore sul null, se foo = null foo.equals(foo2) => NullPointerException
  • "==" viene ridirezionato su "equals", esegue prima un controllo sul null e poi passa ad equals
  • per un controllo del riferimento bisogna usare "eq"

Nota 1: 1.equals(BigInt(1)) = false mentre BigInt(1).equals(1) = true perche' nel primo caso la primitiva va in controllo solo su un altra primitiva

Nota 2: 3 == BigInt(3) e' true BigInt(3) == 3 anche e' true

Examples:

  • 1 equals 2 will return false, as it redirects to Integer.equals(...)
  • 1 == 2 will return false, as it redirects to Integer.equals(...)
  • 1 eq 2 will not compile, as it requires both arguments to be of type AnyRef
  • new ArrayList() equals new ArrayList() will return true, as it checks the content
  • new ArrayList() == new ArrayList() will return true, as it redirects to equals(...)
  • new ArrayList() eq new ArrayList() will return false, as both arguments are different instances
  • foo equals foo will return true, unless foo is null, then will throw a NullPointerException
  • foo == foo will return true, even if foo is null
  • foo eq foo will return true, since both arguments link to the same reference