0

I have a method that receives a parameter which can be null. I have used instanceof to check the type but if the parameter comes null, instanceof returns null. I have read the documentation and it is how instanceof works. In this case , I want to check the type of the passed parameter even if the value is null. example

public static void checkType(Object yourObject) {
    if(yourObject instanceof String) {  // this fails if null passed, even if declared as String
    print("You have passed a String");    
    }
}

What could I do in these cases?

My goal is, that:

String myvar = null; 
checkType(myvar); 

would print:

"You have passed a String"

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
Leo
  • 1,829
  • 4
  • 27
  • 51
  • I don't think you can check the type of a null reference, even at runtime. – Hopey One Mar 31 '21 at 23:46
  • 4
    `null` doesn't have a type. `String a = null;` is the same as `Integer b = null;`, they're both null. In Java (and most OOP) you have to separate the type of the variable from the type of the object the variable holds. For example, you could say `Object obj = "abc";`. Does that mean `"abc"` is not a string, just because you didn't declare it as a string? No. `"abc"` is still a string, you're just referencing it as an object. Same goes here. `null` is still `null`, no matter what you reference it as. – Charlie Armstrong Mar 31 '21 at 23:47
  • not a duplicate, but you may or may find it useful: https://stackoverflow.com/questions/61104708/typeof-in-java-8 – tevemadar Apr 01 '21 at 13:35

2 Answers2

5

Let's say your goal is to have this code, with the output as indicated in the comments:

String s = null;
Integer i = null;
checkType(s); // prints "String"
checkType(i); // prints "Integer"

Then this is impossible unless the two invocations of checkType are actually invoking different methods with the same name, i.e. overloads:

static void checkType(String s) {
    System.out.println("String");
}
static void checkType(Integer i) {
    System.out.println("Integer");
}

This doesn't seem very useful, though, because the method is not actually checking the type; it's the compiler that's checking the type and calling the appropriate overload. You can't get the same result with a single method doing a runtime type check, because the value null doesn't carry the information you want at runtime; there aren't different "flavours" of null, all null values are indistinguishable.

kaya3
  • 47,440
  • 4
  • 68
  • 97
0

You could wrap your parameter in a generic type

public class GenericClass<T> {

  private final Class<T> type;

  public GenericClass(Class<T> type) {
    this.type = type;
  }

  public Class<T> getMyType() {
    return this.type;
  }
}

from this question.

Hopey One
  • 1,681
  • 5
  • 10
  • 1
    This is not an answer to the problem the OP is having. The answer is simply "you can't do that". – CryptoFool Apr 01 '21 at 00:13
  • @CryptoFool, the OP has asked for alternatives to what they have tried in the post. It seems they are well aware that "you can't do that". So if a null reference contains no runtime type information, the only alternative I can think of (with my limited imagination) is to include type information with the reference. – Hopey One Apr 01 '21 at 00:35