This is an interview question:
Suppose you are coding for an Android system which utilizes JNI and some C++ code to read data from a serial interface (uart, for example).
The bottom level stuff will be taken care of by the C++ code, and eventually data will be fed to a Java interface where it will be treated as the type String.
The question is: suppose the Java code handling the reading goes something like this:
private void parseSerialData(String input){
if (input==null){
//DO SOMETHING HERE
}
}
Will it ever be possible for the conditions of the if block be met?
My understanding of Java tells me it is never going to be possible because (I could be thoroughly wrong) null in Java is way to denominate "there is no reference", null isn't an object, and it wasn't instantiated from a class. At the DVM or JVM level, as long as a variable was declared, a reference was made, even though there may be no memory allocated for it on the stack.
Therefore it is impossible for a local variable as a parameter of a method to not have a reference to begin with, not to mention it was given a reference later on which points at a String object (even though this object may have no information with it, it is nevertheless, a non-null object) and as such, the if condition will never be met.
But I couldn't decide if this question is a trick question and I have omitted something? Especially considering that there's C++ in the fray, and I have no idea what it is like to pass a C++ null reference to Java, if it makes sense at all?
So, will the if condition be met, ever?