aspose file tools*
The moose likes Java in General and the fly likes JDK 1.5 Variable Argument issue . 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 » Java » Java in General
Bookmark "JDK 1.5 Variable Argument issue ." Watch "JDK 1.5 Variable Argument issue ." New topic
Author

JDK 1.5 Variable Argument issue .

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi All ,



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

Why Java denies when i pass more than one variabale arg to one method ?.


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Because the compiler cannot tell when your first varargs list ends and the second varargs list begins.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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:

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Mar 30, 2005
Posts: 225

Dear Rob

Thanks for your valuable information



Opened my eyes

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDK 1.5 Variable Argument issue .