• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Mega cool feature in java 1.5

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guyz!

very nice to see you all here!
I've come to tell ask you about one amazing thing I've met recently:

That's all about java 1.5

we have two methods:
1) method (Object [] o)
2) method (Object o )

Can you tell me without looking below which method will be called when
we write:
method (null)?

Maybe someone could tell the reason of this?

or there can be another option

1) method (Object ... o)
2) method (Object o )





The answer is: method number one

But, at first glance it seems methiod number 2 should be invoked ...

Who can anwer this quiestion?
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all in this var-arg (Object... o), o is nothing but a one dimensional Object array.

Second most specific method will be called.

Object[] < Object (means Object[] array can be assigned to Object)

Object is the mother of all. So Object[] is more specific than Object

So first will be called.

More on this see it here Choosing most specific method

Naseem
[ July 13, 2006: Message edited by: Naseem Khan ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the same if the two alternatives were:
1) method (String o)
2) method (Object o )

A String is an Object, so method 1 would be called because String is the more specific.

Nothing "mega-cool" here... just "always been there" overloading rules.

If you want mega-(un?)cool, go visit the Java 5.0 Generics floor.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, Alex, welcome to Java Ranch!
Feel free to browse around some of the earlier topics in the Forum and use our lousy (I mean it) search facilities for any topics you may want to discuss.
Cheers
-Barry
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is reason behind that null will be assigned to Object array(Object... o)?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by gowher amin naik:
what is reason behind that null will be assigned to Object array(Object... o)?



Naseem has already explained that and he has also given the reference to the Java Language Specification.
 
alex ivanou
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your replies. They were useful for me. Links also
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!