aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Null values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Null values" Watch "Null values" New topic
Author

Null values

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Hi...

what are the key points concerning passing and returning null values to and from methods??


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

You can return a null value for any return type other than a primitive type. But while passing, you have to keep in mind the inheritance thing. Suppose there are two methods

method1(Object obj)

and

method1(String obj)

then passing null as method(null) will call the second method as String is lower in the hierarchy. But if you have these methods

method2(String obj)

and

method2(Integer obj)

then passing null as method2(null) will result in a compilation failure. This is because String and Integer don't have any super class sub class relationship, so the compiler will not be able to figure out which method to call...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


SCJP 6
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Ok...

So.. should return false,am I right?

@Ankit

If I have these methods:


And I invoke:



Then I will get a compiler failure.Even though Object is the superclass of both.
But if I remove the String method,then Cat can accept the null?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

null instanceof any class will return false.

In the second question, the problem is between

method(Cat cat)
method(String thing)

Yes object is a super class of both of these, so the compiler would say that OK, I will not call method(Object). But the compiler will not be able to decide which method among these two should be called...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes Cat will accept the null.

For null argument all Objects should in a single hierarchy, if sibling found there is gotcha...

Object
| |
String Cat
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

It's not about siblings. It's only about being in the same hierarchy. If there is a null value being passed, then the compiler will match the lowest match in a single hierarchy.

method(Object)
method(Number)
method(Integer)

Here when you pass null, the compiler will see that all the three classes are in the same hierarchy, so it will find the lowest class in the hierarchy and call that method. But in this code

method(Integer)
method(String)

Here Integer and String both are from different hierarchies. It doesn't matter whether they are at which level of the hierarchy. For example String is a sub-type of CharSequence, Serializable etc and Integer is a sub-type of Number, Serializable etc, but they don't share a super type sub type relationship. So the compiler will not be able to figure out which is the more specific type...
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598


But if I add an additional method:
method(Cat cat)
method(Object)
method(Number)
method(Integer)

Now there is one method that doesn't fit in the hierarchy..will it fail if I pass null?
Or will it still choose Integer?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Duran Harris wrote:
But if I add an additional method:
method(Cat cat)
method(Object)
method(Number)
method(Integer)

Now there is one method that doesn't fit in the hierarchy..will it fail if I pass null?
Or will it still choose Integer?


Did you try this code?? It will fail compilation as Cat is not in the Hierarchy of Object->Number->Integer. So the compiler will not be able to choose between Cat and Integer. Even if Cat is directly a sub class of Object and Integer an indirect sub class, but still Integer will not be chosen. Integer is not specific than Cat...


[Thumbnail for exp.JPG]

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Sibling case should also be there na Ankit. Here Dog and Cat are sibling at the same hierarchy. So compiler is confused here.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
It's simply,

1) If not in the same hierarchy then compiler error, ambiguous situation.

2) If in same hierarchy but Siblings, then again error, ambiguous situation.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

But when you create Dog and Cat from animal, then you will say that Cat has a separate hierarchy than Dog. It's not about the hierarchy of class Animal. It's about the hierarchy of Dog and Cat separately...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
But another simple way to see it as siblings, you are saying the same thing. If I include method(Animal), then seeing sibling would be more simple. Why to complicate the things?




Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Punit Singh wrote:Why to complicate the things?


I am not complicating anything or am I . That's the way it is. You can remember it as siblings but there are enormous things that can happen. Like String and StringBuffer both implement CharSequence. Integer, Double, String are all Serializable and Comparable. So you would say that they are in the same hierarchy. Number also implements Serializable but Number and Integer are not siblings. So I would suggest not to think of that matter as a sibling thing. There might have been sub-classes of Dog and Cat eg. BlackDog, WhiteDog, BlackCat and WhiteCat. Then you would consider them as a separate hierarchy.

So it's just completely a matter of how you want to see it. Some people might agree with your sibling thing, and some might not. I am from the not community in this regard that's the difference
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ok , everything will work. The final destination is to identify the error, you can do that by many ways.
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

It's ok...

Crystal clear now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null values