Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

Why is this possible?

Thread view: 
James Yong - 28 Sep 2005 04:54 GMT
Hi,

I have the following methods:

   public static String method1( Object obj ){
        return method2( new Object[]{ obj } );
   }

   public static String method2( Object[] obj ){
        return "";
   }

if I change to the following, it will still compiles fine. But I was hoping
for an error

   public static String method1( Object obj ){
        return method1( new Object[]{ obj } );
   }

   public static String method2( Object[] obj ){
        return "";
   }

In the examples above, I changed
return method2( new Object[]{ obj } );
to
return method1( new Object[]{ obj } );

Why is this possible?

Regards,
James
Owen Jacobson - 28 Sep 2005 05:05 GMT
> Hi,
>
[quoted text clipped - 7 lines]
>          return "";
>     }

...

> In the examples above, I changed
> return method2( new Object[]{ obj } );
> to
> return method1( new Object[]{ obj } );
>
> Why is this possible?

Arrays are objects (derived from java.lang.Object) too.

-O
James Yong - 28 Sep 2005 08:40 GMT
> > In the examples above, I changed
> > return method2( new Object[]{ obj } );
[quoted text clipped - 6 lines]
>
> -O

Hi Owen, thanks for the explanation.

Regards
Roedy Green - 28 Sep 2005 06:29 GMT
>if I change to the following, it will still compiles fine. But I was hoping
>for an error
>
>    public static String method1( Object obj ){
>         return method1( new Object[]{ obj } );
>    }

recursion is legal is java, even if you have made no provision for the
nesting to terminate.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 28 Sep 2005 06:33 GMT
>    public static String method1( Object obj ){
>         return method1( new Object[]{ obj } );
>    }
Another answer, new Object[] {obj} is also an Object so fits the
method signature.

This are recursively adding layers of indirection.

Just what sort of complaint were you expecting the compiler to make?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

James Yong - 28 Sep 2005 08:34 GMT
> >    public static String method1( Object obj ){
> >         return method1( new Object[]{ obj } );
[quoted text clipped - 5 lines]
>
> Just what sort of complaint were you expecting the compiler to make?

Because I think of another scenario like below. There are two methods of the
same name but different signature.

   public static String method1( Object obj ){
   ............
    }

   public static String method1( Object[] obj ){
    ............
    }

For example if I were to execute
    method1(new Object[]{"1", "2"});
method1( Object obj ) get called if I forget to define method1( Object[]
obj )

That is why I expect the compiler to give a warning or error when the
example above happens.
Thomas Schodt - 28 Sep 2005 09:14 GMT
> Because I think of another scenario like below. There are two methods of the
> same name but different signature.
[quoted text clipped - 14 lines]
> That is why I expect the compiler to give a warning or error when the
> example above happens.

Maybe you want something like

  public static String method1(Object obj){
    if (obj==null) return "";
    return obj.getClass().isArray()?method1a(obj):method1o(obj);
  }
  private static String method1a(Object[] obj){
    ...
  }
  private static String method1o(Object obj){
    ...
  }
James Yong - 28 Sep 2005 14:12 GMT
> > Because I think of another scenario like below. There are two methods of the
> > same name but different signature.
[quoted text clipped - 27 lines]
>      ...
>    }

Hi Thomas,

hmm..I never thought of this approach before.

obj.getClass().isArray()  is new to me. haha. thanks

Regards,
James
Roedy Green - 28 Sep 2005 09:52 GMT
>    public static String method1( Object obj ){
>    ............
[quoted text clipped - 3 lines]
>     ............
>     }

In this case the compiler does a best fit.  It looks for the most
specific fit.  So  String[] goes with the second but String goes with
the first
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

James Yong - 28 Sep 2005 14:05 GMT
> >    public static String method1( Object obj ){
> >    ............
[quoted text clipped - 7 lines]
> specific fit.  So  String[] goes with the second but String goes with
> the first

Hi Roedy,

Thanks for the explanation.

Regards,
James
Roedy Green - 28 Sep 2005 09:56 GMT
>For example if I were to execute
>     method1(new Object[]{"1", "2"});
[quoted text clipped - 3 lines]
>That is why I expect the compiler to give a warning or error when the
>example above happens.

If it did that, to be consistent, any method( Object) would generate a
warning every time any parameter were used but Object.  Taking that
further, every method (Interfacexx ) would generate a warning.  Every
time you used any subclass of the class defined in the method as a
parameter you would get a warning.  The compiler would be crying wolf.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.