When adding system function does not compile [closed]

2

I have the following class:

package br.com.xti.ouvidoria.helper;

import java.util.Collection;

/**
 * @author Samuel Correia Guimarães
 */
public class ValidacaoHelper {

    /**
     * @param obj
     *            objeto a ser validado
     * @return TRUE se o objeto passado por parâmetro for NULL, VAZIO
     *         (Collections, Arrays, Strings) ou menor/igual a zero para tipos
     *         númericos
     */
    public static boolean isEmpty(Object obj) {
        boolean vazio = false;

        if (obj == null) {
            vazio = true;
        } else {
            if (obj instanceof String) {
                vazio = ((String) obj).replaceAll("\s+", " ").trim().isEmpty();
            } else if (obj instanceof Number) {
                if (((Number) obj).longValue() == 0)
                    vazio = true;
            } else if (obj instanceof Collection<?>) {
                Collection<?> col = (Collection<?>) obj;
                vazio = (col == null || col.isEmpty());
            } else if (obj instanceof Object[]) {
                vazio = (((Object[]) obj).length == 0);
            }

        }
        return vazio;
    }

    /**
     * @param objs
     *            objetos a serem validados
     * @return TRUE se algum dos objetos passados por parâmetro for NULL ou
     *         VAZIO
     */
    public static boolean isEmpty(Object... objs) {
        boolean isEmpty = false;
        for (Object obj : objs) {
            isEmpty = isEmpty(obj);
            if (isEmpty) {
                break;
            }
        }
        return isEmpty;
    }

    /**
     * @param obj
     *            objeto a ser validado
     * @return TRUE se o objeto passado por parâmetro for diferente de NULL,
     *         VAZIO (Collections, Arrays, Strings) e maior que zero para tipos
     *         númericos
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

I wanted to add the following function to it:

public static boolean saoIguais(Integer obj, Integer obj2) {
    boolean iguais = false;

    if (obj == obj2) {
        iguais = true;
    } 
    return iguais;
}

But the system is neither compiled!

If I put as Object in place of Integer it compiles:

public static boolean saoIguais(Object obj, Object obj2) {
    boolean iguais = false;

    if (obj == obj2) {
        iguais = true;
    } 
    return iguais;
}

But in this case the comparison never returns true.

    
asked by anonymous 15.09.2017 / 20:28

2 answers

2

The == operator on non-primary types compares only the object reference, not its equality. When you do obj1 == obj2, you are only comparing if the obj1 pointer points to the same obj2 memory address, regardless of the value of each of the objects. To compare equality, use the equals method:

    boolean saoIguais = obj1.equals(obj2);
    
20.09.2017 / 18:58
0

The comparison error is this even though Orlando replied, when you compare a variable of complex types like the objects you compare your memory references. To compare if the values are equal the correct is you override the equals method. But in your case like this comparing an Integer it already overwrites the equals and compares the contents of the object.

I took your method and made an implementation with the given suggestion and validating the nullability of the object:

    public static boolean saoIguais(Integer obj, Integer obj2) {
    //Se quiser considerar que os dois valores nulos são iguais então use o if abaixo
    if(obj == null && obj2 == null )
        return true;
    //Este if é obrigatório para validar se o objeto é nulo
    if(obj == null || obj2 == null )
        return false;
    //Compara os valores usando a implementação equal da classe Integer
    return obj.equals(obj2);
}
    
27.09.2017 / 17:31