Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null values

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

what are the key points concerning passing and returning null values to and from methods??
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
exp.JPG
[Thumbnail for exp.JPG]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok , everything will work. The final destination is to identify the error, you can do that by many ways.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's ok...

Crystal clear now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic