Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading constructors

 
Dirk Neefs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem when invoking overloaded constructors when I pass null as a parameter. This is my code:

When I call isString() it returns false.
My question now is: what are the rules to invoke an overloaded constructor and what is the impact on this for the null value?
[ May 22, 2003: Message edited by: Cindy Glass ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Dirk, and Welcome to JavaRanch.
In general, answer to your question lies in the fact that null is considered to be a very specific value, and when faced with overloaded methods, the most specific one will be called.
(My examples below are methods, but I believe that the same holds true for constructors)
That is, if you had:

and called aMethod(null), it would print "Integer" since that is the most specific method. However, if you also had

and called aMethod(null), you would get a compile-time error, since the compiler cannot tell what you class you mean for null to be (Integer and String are of equal specific-ness). You would have to cast your null, i.e., aMethod((String)null).
However, when I try to compile you code, I get the following errors:

Of course this is because byte[] and char[] are not Strings in Java.
[ May 22, 2003: Message edited by: Joel McNary ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic