0

This could be quite a stupid question, but I am not sure how to solve this.

int num;
String str;
float flt;

I define three variables, one int, one String and one float. Than I do the following:

Object a = num;
Object b = str;
Object c = flt;

How can I check the a, b or c object for the default value? I know that it is null for String, 0 for int, and 0.0 for float, but is there a way to do some sort of:

if(a==nullValue(a) && b==nullValue(b) && c==nullValue(c))

so that it is equal to:

if(a==0 && b==null && c==0.0)

I just get the three a, b, c objects passed and so I don't know which type they are.

11
  • Are those initial variables fields? Commented Apr 7, 2014 at 20:48
  • Yes they are private non initialized fields in a class. Commented Apr 7, 2014 at 20:49
  • What do you mean "I don't know which type they are"? What is the type of the variable you are using to refer to each? Commented Apr 7, 2014 at 20:50
  • if o.getClass().isPrimitive() then if clazz.equals(int.class) then return 0 ... and others else if not primitive return null Commented Apr 7, 2014 at 20:52
  • 1
    @MarekRaszewski o.getClass().isPrimitive() will never return true in this case. Commented Apr 7, 2014 at 20:56

3 Answers 3

2

Java is a strongly typed language and you are trying to defeat and not utilize one of its core strengths.

Technically speaking, you can achieve what you want, generally you will have to test your Object obj against all possible primitive types + null (since you don't know what this Object really is ):

     static boolean isDefaultValue(Object obj) {

        if (obj == null // non-primitive
                || obj.equals((byte) 0) // default for byte
                || obj.equals((short) 0) // short
                || obj.equals((int) 0) // int
                || obj.equals(0L) // long
                || obj.equals(0.0f) // float 
                || obj.equals(0.0d) // double
                || obj.equals('\u0000') // char 
                || obj.equals(false)) { // boolean

            return true;
        }
        return false;
    }

However, instead of doing this, I really encourage you to revise your design and ask yourself why you completely erase data types by casting everything to Object. This could lead to unreadable and highly dangerous code.

Post your reason, and we might help you find better solution.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for using polymorphism here, rather instanceof or getClass.
I have a method which returns the value of a field using reflection. Since the attribute could be of any type, my method returns it as an object. When I later want to check if it "has a value" I have to use such a method to cover all possible types.
ok, perhaps your scenario with reflection is a valid use case. You could use my or David's answer(doing exactly same but with less code). Also, consider, that while extracting attribute value with reflection as you described, you also know its type at this point and could use this type info to check for default of this type right away. I guess it will be tiny bit more performant than looping over 9 possible default type/value tuples.
1

Add this to the class of your choice.

private static final List<? extends Object> DEFAULTS = 
    Arrays.asList( null, (byte) 0, (short) 0, 0, 0L, 0F, 0D, '\u0000', false );

private static boolean isDefault(Object o) {
    return DEFAULTS.contains(o);
}

1 Comment

+1 love it. Looks like improved version of my answer. You could also get rid of wildcard and have List<Object> if you cast your null to Object explicitly.
-1

For type checking you should use instanceof like this

Object obj = null;
        int num = 0;
        String str = null;
        float flt = 0f;

        obj = num;
//      obj = str;
//      obj = flt;
        if(obj instanceof Integer){
            //THEN IT SHOULD BE 0
        }
        else if(obj instanceof String){
            //THEN IT SHOULD BE NULL
        }
        else if(obj instanceof Float){
            //THEN IT SHOULD BE NULL
        }
        else{
            //IT IS NULL BECAUSE Object obj = null;
        }

It works for any type.

2 Comments

No, this doesn't work at all. instanceof always returns false if the left hand side is null.
You are right, if the left side is null then it isn't an instance of anything. But you only have this problem with types that aren't primitive types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.