Java how to compare "float" to "null"?

Asked

Viewed 309 times

7

public class NuloOuNao {
    public static boolean isZero(float num) {
        if(num == null) {
            return true;
        }else {
            return false;
        }
    }
    public static void main(String[] args) {
        isZero(10);
    }
}

Row 5: The Operator == is Undefined for the argument type(s) float, null

1 answer

11


Java has types by value as is the case float. These types cannot be null. But Java created types by reference, that is, types created as classes, equivalent to primitive types. So there’s the guy Float. He’s essentially the same as float, but it is an isolated object. It is much less efficient in every way, but has a natural null value.

public class NuloOuNao {
    public static boolean isZero(Float num) {
        return num == null;
    }
    public static void main(String[] args) {
        isZero(10);
    }
}

I put in the Github for future reference.

Think carefully if you need it. This code seems to do something misleading. Null and zero are very different things.

Papel higiênico vazio e sem rolo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.