Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Wiki MarkupThe Java compiler type-checks the arguments to each varargs method to ensure that the arguments are of the same type or object reference. However, the compile-time checking is ineffective when {{Object}} or generic {{T}} parameter types are used \ [[Bloch 2008|AA. References#Bloch 08]\]. Another requirement for providing strong compile-time type checking of variable argument methods is to be as specific as possible when declaring the type of the method parameter.

Noncompliant Code Example (Object)

...

Code Block
bgColor#ccccff
ReturnType method(SpecificObjectType... args) { }

...

Retrofitting old methods containing final array parameters with generically typed varargs is not always a good idea. For example, given a method that does not accept an argument of a particular type, it could be possible to override the compile-time checking --- through the use of generic varargs parameters --- so that the method would compile cleanly rather than correctly, causing a compile-time error \[ [Bloch 2008|AA. References#Bloch 08]\].

Also, note that autoboxing does not allow strong compile-time type checking of primitive types and their corresponding wrapper classes.

...

Automated detection appears to be straightforward.

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2a4c136a-451d-4071-843c-bf68aec6c7ab"><ac:plain-text-body><![CDATA[

[[Bloch 2008AA. References#Bloch 08]]

Item 42: "Use Varargs Judiciously"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a5635f5b-ccd1-488a-986f-1b920695d76b"><ac:plain-text-body><![CDATA[

[[Steinberg 2005AA. References#Steinberg 05]]

"Using the Varargs Language Feature"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8fe6b6eb-82fd-4d75-8179-47060f2d45f2"><ac:plain-text-body><![CDATA[

[[Sun 2006AA. References#Sun 06]] [varargs

http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html] ]]></ac:plain-text-body></ac:structured-macro>

...

DCL52-J. Avoid ambiguous overloading of varargs methods      01. Declarations and Initialization (DCL)      DCL57-J. Do not derive a value associated with an enum from its ordinal