Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding generic, var-arg overload

 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to invoke var args arraylist version you can do something like this:
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried both , -Xlint:none and -nowarn but it again gives the warning . what to do ??
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your explanantion rushikesh .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic