Arne Vajh?j wrote:
Seamus MacRae wrote:
Thomas A. Russ wrote:
Seamus MacRae <smacrae319@live.ca.nospam> writes:
Adlai wrote:
You can have a function that returns some data, or
NIL if the data is unavailable.
Java can do this (return an object or null), and java.util.Map.get()
does it and is in the standard library.
Um, how does this work if you want the Java method to return a literal
such as int or double? I guess you are then forced to wrap the return
type in the corresponding Integer or Double, so that you have a safe
null value you can return?
Not since version 5.
Also in version 5 and 6.
null is not a valid value for int or double - it requires
Integer or Double.
And again someone jumps at an apparent opening in order to attack me
without actually reading carefully first.
Bad luck, Arne.
My remark:
was in response to:
Um, how does this work if you want the Java method to return a literal
such as int or double? I guess you are then forced to wrap the return
type in the corresponding Integer or Double, so that you have a safe
null value you can return?
as is evident in the quoted material at the start of this post. And it
is quite true that, since version five, to return a literal int or
double you are not forced to wrap the return type. You can have, say,
boolean fooAvailable;
int foo;
...
public Integer getFoo () {
if (fooAvailable) return foo;
return null;
}
and the compiler will be happy to do the wrapping for you. So you are no
longer forced to wrap anything -- sometimes the compiler does it for you.
You do still need to declare the return type as Integer or Double. But
the type of the expression in the "return" statement may now be int or
double.
In particular, if you want to return a literal, which was the particular
case at issue here, you can.
Pre-Java 5:
public Integer returnOneOrNull () {
if (foo) return Integer.valueOf(1);
return null;
}
Java 5 and later:
public Integer returnOneOrNull () {
if (foo) return 1; // An int literal is returned.
return null;
}
Nice try. Next time you want to pick a fight with someone, read more
carefully and choose your target more carefully, or better yet, think
better of it and just go on about your business.
I think you should read more carefull yourself.
is being discussed.
to be able to return null.
Before 1.5 and after.