casting - Java Type Cast, Object Type and overloading Issue -


please have on following class, need check if there valid value in variable. works fine if there proper value in variable instead of null, when comes null behaviour not expect (although might make sense if integer = null; when checked a instanceof integer,

can 1 guide me how achieve correct result following class?

package com.mazhar.hassan;  public class valuechecker {     public static boolean empty(integer value) {         system.out.println("integer");         return (value != null && value.intvalue() > 0);     }     public static boolean empty(long value) {         system.out.println("long");         return (value != null && value.longvalue() > 0);     }     public static boolean empty(string value) {         system.out.println("string");         return (value != null && value.length() > 0);     }     public static boolean empty(object value) {         system.out.println("object");         return (value != null);     }     public static void checkall(object... args) {         for(object o: args) {             if (o instanceof integer) {                 empty((integer)o);             }             else if (o instanceof long) {                 empty((long)o);             }             else if (o instanceof string) {                 empty((string)o);             }             else {                 empty(o);             }         }     }     public static void main (string[] args) {         integer = null;         long b =  null;         string x = null;         object y = null;          if (a instanceof integer) {             system.out.println("a integer");         } else {             system.out.println("a not integer");         }          system.out.println("/---------------------------------------------------/");         checkall(a,b,x,y);         system.out.println("/---------------------------------------------------/");         empty(a);         empty(b);         empty(x);         empty(y);     } } 

why need exact type checking is, thave throw erros "not valid integer", "no valid long" etc.

the output of above class follows.

/-----------------------(output 1)----------------------------/ not integer /-----------------------(output 2)----------------------------/ object object object object /------------------------(output 3)---------------------------/ integer long string object 

output 1: not integer (checked instanceof) can not recognize when passed overloaded function goes right function (output 3)

output 2: how achieve checkall multiple/dynamic param checkall(varint, varlong, varstring, varobject)

the behaviour output 1 caused fact method overloads bound @ compile time. specific overload pick bound before program runs. instanceof, on other hand, runtime check.

thus, @ runtime a instanceof integer null instanceof integer, false.

but each of individual method calls, proper method called because compiler bound specific overload of method @ compile time, based on reference type of variable. thus:

empty(a); // compiled call empty(integer value) empty(b); // compiled call empty(long value) empty(x); // compiled call empty(string value) empty(y); // compiled call empty(object value) 

so regardless of actual object a, b, x, , y reference, you'll right output on console respective object.


output 2: how achieve checkall multiple/dynamic param checkall(varint, varlong, varstring, varobject)

well, if you're going pass null, can't really. null null @ runtime, , doesn't have type information associated it. jvm can't tell 1 null "string null" or "object null". it's null. can't implement multiple check want null inputs -- null instanceof ______ return false, you'll end default case.

if pass actual objects, though, method should work properly.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -