aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding generic, var-arg overload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding generic, var-arg overload" Watch "regarding generic, var-arg overload" New topic
Author

regarding generic, var-arg overload

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Hi guys,

the following code shows an illegal overload :



This is because the compiler cannot distinguish between the arguments of the two methods. (correct!!)

But how come this compiles:




Ok . This means that the compiler can distinguish between ArrayList<String> and ArrayList<String>... (var-arg). In the above code I have invoked the ArrayList<String> method. can anyone tell me how to invoke the ArrayList<String>... (Var-arg) method ? Please insert a line in my code


SCJP 6.0 (86%) SCWCD 5 (94%)
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
no because var args is equivalent to array.
so, in first case it sees both the arguments as arrays and in the second case one argument as array and other as a single arraylist.


SCJP 1.6 96%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
to invoke var args arraylist version you can do something like this:
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
oh i got it .. but neha the code you wrote is giving warning for unchecked and unsafe operation. How am i going to compile it and verify the result? by the way why is the compiler warning about unsafe operation when i have written <String> in the method signature and everywhere else.
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi,

saima kanwal wrote:oh i got it .. but neha the code you wrote is giving warning for unchecked and unsafe operation. How am i going to compile it and verify the result? .


I guess
javac -Xlint:none YourProgram.java should work
or
javac -nowarn YourProgram.java

Take a look at this http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html

Blog
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
I have tried both , -Xlint:none and -nowarn but it again gives the warning . what to do ??
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331

oh !

but then if I'm not wrong, the .class files should still get created despite compiler warnings. so you must be able to execute the code.

As far the reason for the warning, I think this might be the reason..

The compiler considers,



as something like



(since var-args are internally an array )

and now the type information is lost... So passing ArrayList<String> objects is considered as unsafe... Just my thought...


saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
well you may be right Vishwanath , but what i figured out is that there is no problem with the method signature containing ArrayList<String>... The warning comes when i pass two arraylists, mylist1 and mylist2 together, and when i pass a single list mylist1 or mylist2 , there are no warnings .
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
in the given code, the var-arg, at runtime, tries to create something like:

ArrayList<String>[]a=new ArrayList<String>[5];

which is not allowed at compile time. It flags an error, "generic array creation".
Since compiler knows that this might happen at runtime, it gives warning for vararg method.


SCJP 5.0 100%
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
thanks for your explanantion rushikesh .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding generic, var-arg overload