-1

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?

joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • 2
    Because they are different languages based on different language rules they are bound to show different behavior. – Alok Save Jan 22 '13 at 14:49
  • `this behaviour be different in C/C++` – Guy P Jan 22 '13 at 14:50
  • `obj.anything` when `obj` is null won't work. `.equals` is different than `==` in almost every case. – chris Jan 22 '13 at 14:50
  • @AlokSave In which language we can try this out? – joey rohan Jan 22 '13 at 14:51
  • 2
    The three languages mentioned handle things totally differently. You should ask about one at a time. – Jon Jan 22 '13 at 14:51
  • @Guy can you give a Example code? – joey rohan Jan 22 '13 at 14:51
  • `return (Object)void` <- `void` is not an expression. – Daniel Fischer Jan 22 '13 at 14:52
  • Note - `a == null`, `b != null` -> `b.equals(a)` should work (and return false) (as long as whomever coded the method is a decent coder). – Bernhard Barker Jan 22 '13 at 14:54
  • 2
    In C++, as in Java, you can't call member functions (aka methods) on a null pointer (or dereference it in any other way), but you get undefined behaviour rather than an exception. In C, there's no equivalent to methods, but dereferencing a null pointer again gives undefined behaviour. In all three languages, `void` is a type name, not a valid expression, so it can't be cast to anything. – Mike Seymour Jan 22 '13 at 14:56

3 Answers3

2

You can not call equals on null instead you should use ==. Note that calling == and equals is not the same. == compares references while equals is 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:

 if( *obj == *obj)

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

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

1)

if(obj.equals(obj))

It's true that the default equals implementation just uses == to check if the two objects (actually, the two references) are actually referring to the same object in memory, but it's nonetheless a call to an instance method, which is bound to fail with a NullPointerException if it's called over a null.

 if(null==null)

instead doesn't give any problem, since you are not calling any instance method, you are just comparing addresses.

2) void is just a keyword used to mark a function returning nothing1; it doesn't make any sense to try to return it. null, instead, marks an object reference as pointing to nothing, that's an entirely different concept.

3) In C++ there's no default "equals" method, and the whole object model is based on value types (in Java, instead, you normally work with references to objects), so it's difficult to make a precise comparison.2

In C++ an object (or a reference) is guaranteed to be a "valid" object (=there's space allocated for it and a constructor has run), so there's no possibility of getting an hypothetical equivalent of NullPointerException when calling an instance method over an object.

On the other hand, it's entirely possible to have a pointer that is NULL, and in such case you go into undefined behavior (usually a crash) if you try to dereference it in some way (e.g. via * or ->)3.

As for the equality checks: the == operator for objects is not implemented by default, so comparing objects for equality makes sense only if the implementor of the class actually wrote an overload for operator==; as for pointers, the == operator is provided by the language itself (and cannot be overridden), and it actually compares the two addresses.


  1. In contrast to C and C++, where void is considered a type (in particular, "an incomplete type that cannot be completed"), in Java it's not, although in some respects it can be considered as such.
  2. In C there are no classes or methods period - it's not an OO language; still, most of the discussion that follows applies to structs and accessing their fields.
  3. Although... if an instance method isn't virtual and doesn't use any field of the object (=the this pointer isn't dereferenced in any way), in most implementations you may get away with calling an instance method on a NULL pointer. Also, by "formally dereferencing" a pointer you can get away with creating a "null reference", that will probably cause trouble when it's used. But in both cases it's still undefined behavior - i.e., it's not correct, although no preventive checks are normally performed, favoring performance over strict rules enforcing.
Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

(Object)null is null, meaning you cannot use any methods on it as it does not exist.

As for the syntax error, in Java void is a type,not a value, so you cannot return void.

André Oriani
  • 3,553
  • 22
  • 29
ldam
  • 4,412
  • 6
  • 45
  • 76
  • Can we do this in c/c++? – joey rohan Jan 22 '13 at 14:57
  • @joeyrohan: No, you can't dereference null pointers or use `void` as an expression in those languages either. – Mike Seymour Jan 22 '13 at 14:58
  • Older languages like Pascal would make distinction between procedures and functions. Both were parameterized code routines, but function would be allowed to return a result to caller in analogy to mathematical functions. C and latter languages unified the concept creating the `void` type to mean that the function does not return a value – André Oriani Jan 22 '13 at 16:33