• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDK 1.5 Variable Argument issue .

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,



dummyMtd() is valid and dummyMtd2() is not valid .

Why Java denies when i pass more than one variabale arg to one method ?.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the compiler cannot tell when your first varargs list ends and the second varargs list begins.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's a lot harder to determine where the first vararg list ends and the second one stops, than just simply start the vararg list and only stop when you take all parameters.

Granted, in this example it could have been possible to have 2 sets of varargs, but the general rule would become more complex. Sun would need to add exceptions to the general rule for things like the following:
As you see, there would be much confusion, both for the compiler and the programmer. You could specify in the Java Language Specification that the two vararg lists should be 100% incompatible, but that's actually impossible*. Consider:
It seems that Serializable and List<?> are incompatible - but you can create your own class that makes them both compatible.

* Ok, impossible may be too harsh, but because of these problems there would be quite a few limitations. For instance:
- none of the vararg types can be an interface; with the exception of final classes, any class can be subclassed so it implements an interface
- none of the vararg types can be a super class of any of any of its adjacent vararg types (see the Integer / Number example)
- etc

The varargs rule would become so complex that programmers would get confused, and the compiler would need to be rewritten as well - it can't simply collect varargs until there are no more left, it needs to check each next vararg to see if it's part of the current varargs list or the next one.


So instead of two varargs lists, turn one (or both) into explicit arrays. That's what the compiler does with varargs anyway - it turns it into an array. For example:
When decompiled using JAD, I get this:
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rob & Campbell

Thanks for your reply .

Infact i can able to create the vArg with interface



i have one more doubt ,you had shown the decomipled code of program which uses vArg ,(Since it indirectly converts to array ,why cant we use more than one vArg ) .



So if complier decomplies in above way it would have unserstood ending of a1 array and starting of a2 array.

Please let me know you view .

 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Gowtham wrote:Dear Rob & Campbell

Thanks for your reply .

Infact i can able to create the vArg with interface


You can use any type in varargs, even primitives. But only if it's the only varargs parameter, and it is the last parameter.

i have one more doubt ,you had shown the decomipled code of program which uses vArg ,(Since it indirectly converts to array ,why cant we use more than one vArg ) .



So if complier decomplies in above way it would have unserstood ending of a1 array and starting of a2 array.

Please let me know you view .


Given the following example, can you tell me what ai and a2 should be?
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rob

Thanks for your valuable information



Opened my eyes

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic