tags:

views:

221

answers:

5
+1  Q: 

Java generics

Is it possible to make a method that will take anything, including objects, Integers etc? I have a method checking the value if it is null and I was thinking perhaps it could be done with generics instead of overloading. Unfortunately, trying

nullChecking(Class<? extends Object> value){
...
}

won't allow Integers as they extend Number not object. Is there a way?

Cheers

+5  A: 

Can't you just do nullChecking(Object value) ? If you just wanna test if it's null, it will work.

Valentin Rocher
*slaps forehead* I just listened to a bunch of stuff about generics today so I think I was defaulting to that thinking. Thanks :)Cheers
etc. wouldn't work. I mean int, char, long :)
Adeel Ansari
Yeah it wouldn't work on int, char, long,etc. but these cannot be null, so there is no real problem.
Valentin Rocher
@Vinegar @Bishiboosh: it would work. They'd be autoboxed into Integer, Char, ... and thus always return false ...
Joachim Sauer
Yes but anyway testing them would be useless, since they can't be null.
Valentin Rocher
+1  A: 

I'm not sure what you are trying to do... If it's just checking if a object is null then you can simply do this:

public static boolean nullChecking(Object obj) {
 return obj == null;
}
bruno conde
Yep I am doing something like this, it is simply to throw an error if it is null so it won't return anything and object fits the bill in this case.Cheers
I'd call it ensureNonNull().
starblue
@starblue: I'd expect "ensureNonBlue()" to do something like "if (obj == null) throw new NullPointerException()" or something to the same effect. I wouldn't expect it to simply return some value ...
Joachim Sauer
A: 

A simple, but not very nice solution would be to just call the getClass() method of the object - no need for any additional code (if you just want to throw an Exception).
Advantage: simple, fast and the Exception is not thrown one step away from where you want to check
Disadvantage: harder to understand, just works for throwing a NullPointerException (no logging)

Example:

someObj.getClass();     // check for null
otherObj.getClass();    // "
Carlos Heuberger
not meant to be very serious :--)
Carlos Heuberger
A: 

Actually there is a way to do this, but I wouldn't recommend it for your usage...

So, here is how to do this (and warning, it circumvents compile time type checking, so you open yourself up to run-time class-cast exceptions...)

Say you have a "getter" than can return multiple types - it can be declared as a generic type like so:

public <X> X get(String propertyName) { ... }

You can do the same for a 'setter':

public <X> void set(String property, X value) { ... }
A: 

A

public <T> boolean isNull(T obj) { return obj == null; }

would work... but... what for?

Do you think that

if (isNull(myObj)) { ... }

is easier to write than

if (myObj == null) { .... }

?

Consider you will doing a method invocation in the first case and that consumes [almost nothing, but it does] system resources with no logical advantage, to me.