Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do constructor type arguments mean when placed *before* the type?

I've recently come across this unusual (to me) Java syntax...here's an example of it:

List list = new <String, Long>ArrayList();

Notice the positioning of the <String, Long> type arguments...it's not after the type as normal but before. I don't mind admitting I've never seen this syntax before. Also note there are 2 type arguments when ArrayList only has 1.

Does the positioning of the type arguments have the same meaning as putting them after the type? If not, what does the different positioning mean?

Why is it legal to have 2 type arguments when ArrayList only has 1?

I've searched the usual places, eg. Angelika Langer and on here but can't find any mention of this syntax anywhere apart from the grammar rules in the Java grammar file on the ANTLR project.

like image 767
Nathan Adams Avatar asked Mar 25 '19 02:03

Nathan Adams


People also ask

What are arguments in a constructor?

A Constructor with arguments(or you can say parameters) is known as Parameterized constructor. As we discussed in the Java Constructor tutorial that a constructor is a special type of method that initializes the newly created object.

How arguments are passed to the constructor?

Arguments are passed by value. When invoked, a method or a constructor receives the value of the variable passed in. When the argument is of primitive type, "pass by value" means that the method cannot change its value.

How do you pass a constructor as an argument in Java?

Constructors can be passed as arugments to methods using a method reference, somewhat like a function pointer in C++. This can be a Function type with one argument or a BiFunction type with two arguments, either way its a lambda returning a class of the type it constructs.

How do you fix constructor Cannot be applied to given types?

Constructor X in Class Y Cannot be Applied to Given Types Just as with ordinary methods, constructors need to be invoked with the correct number, type, and order of arguments.


2 Answers

Calling a generic constructor

This is unusual alright, but fully valid Java. To understand we need to know that a class may have a generic constructor, for example:

public class TypeWithGenericConstructor {

    public <T> TypeWithGenericConstructor(T arg) {
        // TODO Auto-generated constructor stub
    }
    
}

I suppose that more often than not when instantiating the class through the generic constructor we don’t need to make the type argument explicit. For example:

new TypeWithGenericConstructor(LocalDate.now(ZoneId.systemDefault()));

Now T is clearly LocalDate. However there may be cases where Java cannot infer (deduce) the type argument. Then we supply it explicitly using the syntax from your question:

new <LocalDate>TypeWithGenericConstructor(null);

Of course we may also supply it even though it is not necessary if we think it helps readability or for whatever reason:

new <LocalDate>TypeWithGenericConstructor(LocalDate.now(ZoneId.systemDefault()));

In your question you seem to be calling the java.util.ArrayList constructor. That constructor is not generic (only the ArrayList class as a whole is, that’s something else). For why Java allows you to supply type arguments in the call when they are not used, see my edit below. My Eclipse gives me a warning:

Unused type arguments for the non generic constructor ArrayList() of type ArrayList; it should not be parameterized with arguments <String, Long>

But it’s not an error, and the program runs fine (I additionally get warnings about missing type arguments for List and ArrayList, but that again is a different story).

Generic class versus generic constructor

Does the positioning of the type arguments have the same meaning as putting them after the type? If not, what does the different positioning mean?

No, it’s different. The usual type argument/s after the type (ArrayList<Integer>()) are for the generic class. The type arguments before are for the constructor.

The two forms may also be combined:

List<Integer> list = new <String, Long>ArrayList<Integer>();

I would consider this a bit more correct since we can now see that the list stores Integer objects (I’d still prefer to leave out the meaningless <String, Long>, of course).

Why is it legal to have 2 type arguments when ArrayList only has 1?

First, if you supply type arguments before the type, you should supply the correct number for the constructor, not for the class, so it hasn’t got anything to do with how many type arguments the ArrayList class has got. That really means that in this case you shouldn’t supply any since the constructor doesn’t take type arguments (it’s not generic). When you supply some anyway, they are ignored, which is why it doesn’t matter how many or how few you supply.

Why are meaningless type arguments allowed?

Edit with thanks to @Slaw for the link: Java allows type arguments on all method calls. If the called method is generic, the type arguments are used; if not, they are ignored. For example:

int length = "My string".<List>length();

Yes, it’s absurd. The Java Language Specification (JLS) gives this justification in subsection 15.12.2.1:

This rule stems from issues of compatibility and principles of substitutability. Since interfaces or superclasses may be generified independently of their subtypes, we may override a generic method with a non-generic one. However, the overriding (non-generic) method must be applicable to calls to the generic method, including calls that explicitly pass type arguments. Otherwise the subtype would not be substitutable for its generified supertype.

The argument doesn’t hold for constructors since they cannot be directly overridden. But I suppose they wanted to have the same rule in order not to make the already complicated rules too complicated. In any case, section 15.9.3 on instantiation and new more than once refers to 15.12.2.

Links

  • Generics Constructor on CodesJava
  • JLS 15.9.3. Choosing the Constructor and its Arguments
  • JLS 15.12.2.1. Identify Potentially Applicable Methods
  • What is the point of allowing type witnesses on all method calls?
like image 196
Ole V.V. Avatar answered Oct 11 '22 22:10

Ole V.V.


Apparently you can prefix any non-generic method/constructor with any generic parameter you like:

new <Long>String();
Thread.currentThread().<Long>getName();

The compiler doesn't care, because it doesn't have to match theses type arguments to actual generic parameters.

As soon as the compiler has to check the arguments, it complains about a mismatch:

Collections.<String, Long>singleton("A"); // does not compile

Seems like a compiler bug to me.

like image 40
svenmeier Avatar answered Oct 11 '22 21:10

svenmeier