Here I have few Questions:
1)I tried to return a null value from a method,something like this(only when TypeCasted into object) :
public static Object returns()
{
return (Object)null ;
}
And stored the Object:
...main()..{
Object obj= returns();
...}
When i checked:
if( obj.equals(obj))
Threw a NullPointerException
But,
if(null==null)
System.out.println("works");
Compiled and gave right output.Any reasons for this?
2) Also, when i tried:
public static Object returns()
{
return (Object)void ;
}
Gave me Syntax Error.Any reasons?
3) Can this behaviour be different in C/C++ ?Or any other OO Language?
You can not call
equalsonnullinstead you should use==. Note that calling==andequalsis not the same.==compares references whileequalsis a method which by convention compares values. As the null object has no value this has no meaning for it.Void is not a value in any language and so it can not be casted to anything(in this case
Object).In C++ to return NULL you will have to return a pointer. To compare the returned object you will have to type something like:
And again this will fail as obj is NULL. In any language I know you will have to do an explicit check for null before comparing.
EDIT: come to think a bit what you try will work in
ruby