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.
java generics syntax constructor generic-type-argument
add a comment |
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.
java generics syntax constructor generic-type-argument
Yeah so do I, I'm not asking how to create a list lol
– Nathan Adams
Mar 25 at 2:58
4
A constructor may have type arguments that are placed there (this particular constructor hasn’t, so<String, Long>
is just ignored). See Generics Constructor.
– Ole V.V.
Mar 25 at 2:58
2
OK that makes sense, although it's weird that there's no compile error even though there's no type arguments on the constructor
– Nathan Adams
Mar 25 at 3:03
5
No error, but you get a warning about raw types. Don't use raw types. Do use the diamond operator.List<String> list = new ArrayList<>();
– Elliott Frisch
Mar 25 at 3:05
@OleV.V. if you wanna put your comment and link as an answer I'll accept it
– Nathan Adams
Mar 25 at 3:06
add a comment |
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.
java generics syntax constructor generic-type-argument
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.
java generics syntax constructor generic-type-argument
java generics syntax constructor generic-type-argument
edited 16 hours ago
Paolo Forgia
4,70262850
4,70262850
asked Mar 25 at 2:55
Nathan AdamsNathan Adams
6531511
6531511
Yeah so do I, I'm not asking how to create a list lol
– Nathan Adams
Mar 25 at 2:58
4
A constructor may have type arguments that are placed there (this particular constructor hasn’t, so<String, Long>
is just ignored). See Generics Constructor.
– Ole V.V.
Mar 25 at 2:58
2
OK that makes sense, although it's weird that there's no compile error even though there's no type arguments on the constructor
– Nathan Adams
Mar 25 at 3:03
5
No error, but you get a warning about raw types. Don't use raw types. Do use the diamond operator.List<String> list = new ArrayList<>();
– Elliott Frisch
Mar 25 at 3:05
@OleV.V. if you wanna put your comment and link as an answer I'll accept it
– Nathan Adams
Mar 25 at 3:06
add a comment |
Yeah so do I, I'm not asking how to create a list lol
– Nathan Adams
Mar 25 at 2:58
4
A constructor may have type arguments that are placed there (this particular constructor hasn’t, so<String, Long>
is just ignored). See Generics Constructor.
– Ole V.V.
Mar 25 at 2:58
2
OK that makes sense, although it's weird that there's no compile error even though there's no type arguments on the constructor
– Nathan Adams
Mar 25 at 3:03
5
No error, but you get a warning about raw types. Don't use raw types. Do use the diamond operator.List<String> list = new ArrayList<>();
– Elliott Frisch
Mar 25 at 3:05
@OleV.V. if you wanna put your comment and link as an answer I'll accept it
– Nathan Adams
Mar 25 at 3:06
Yeah so do I, I'm not asking how to create a list lol
– Nathan Adams
Mar 25 at 2:58
Yeah so do I, I'm not asking how to create a list lol
– Nathan Adams
Mar 25 at 2:58
4
4
A constructor may have type arguments that are placed there (this particular constructor hasn’t, so
<String, Long>
is just ignored). See Generics Constructor.– Ole V.V.
Mar 25 at 2:58
A constructor may have type arguments that are placed there (this particular constructor hasn’t, so
<String, Long>
is just ignored). See Generics Constructor.– Ole V.V.
Mar 25 at 2:58
2
2
OK that makes sense, although it's weird that there's no compile error even though there's no type arguments on the constructor
– Nathan Adams
Mar 25 at 3:03
OK that makes sense, although it's weird that there's no compile error even though there's no type arguments on the constructor
– Nathan Adams
Mar 25 at 3:03
5
5
No error, but you get a warning about raw types. Don't use raw types. Do use the diamond operator.
List<String> list = new ArrayList<>();
– Elliott Frisch
Mar 25 at 3:05
No error, but you get a warning about raw types. Don't use raw types. Do use the diamond operator.
List<String> list = new ArrayList<>();
– Elliott Frisch
Mar 25 at 3:05
@OleV.V. if you wanna put your comment and link as an answer I'll accept it
– Nathan Adams
Mar 25 at 3:06
@OleV.V. if you wanna put your comment and link as an answer I'll accept it
– Nathan Adams
Mar 25 at 3:06
add a comment |
3 Answers
3
active
oldest
votes
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
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?
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
3
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you haveclass Test { <T> Test() {} }
then callingnew <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?
– Slaw
Mar 25 at 4:51
4
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
1
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
1
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
|
show 1 more comment
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.
add a comment |
Here:
List list = new <String, Long>ArrayList();
You have a raw type, as you don't specify a generic type for your list object on the left hand side. Providing the generic type on the right hand side is mostly not required in the first place, as the compiler can defer the generic type.
A bit of guessing here: due to fact that list itself is a raw type, the type specification given to new is somehow ignored. The other answer nicely explains why you can have type parameters in that place. But because a raw type doesn't care about type parameters, the compiler doesn't bother checking that detail here.
2
This doesn’t seem to explain that you can haveList<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even havenew <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.
– Ole V.V.
Mar 25 at 7:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330697%2fwhat-do-constructor-type-arguments-mean-when-placed-before-the-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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?
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
3
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you haveclass Test { <T> Test() {} }
then callingnew <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?
– Slaw
Mar 25 at 4:51
4
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
1
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
1
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
|
show 1 more comment
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
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?
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
3
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you haveclass Test { <T> Test() {} }
then callingnew <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?
– Slaw
Mar 25 at 4:51
4
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
1
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
1
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
|
show 1 more comment
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
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?
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
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?
edited Mar 25 at 10:27
answered Mar 25 at 3:13
Ole V.V.Ole V.V.
32.1k74257
32.1k74257
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
3
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you haveclass Test { <T> Test() {} }
then callingnew <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?
– Slaw
Mar 25 at 4:51
4
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
1
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
1
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
|
show 1 more comment
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
3
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you haveclass Test { <T> Test() {} }
then callingnew <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?
– Slaw
Mar 25 at 4:51
4
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
1
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
1
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
But how it is allowing 2 arguments <String, Long>.The list will allow storing which type of data?
– Jaspreet Jolly
Mar 25 at 3:29
3
3
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you have
class Test { <T> Test() {} }
then calling new <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?– Slaw
Mar 25 at 4:51
What's interesting to me, and sorry if I missed you already mentioning/implying this, is if the constructor (or method) is generic, having the incorrect number of type arguments results in a compilation error—at least for me in OpenJDK 12. For example, if you have
class Test { <T> Test() {} }
then calling new <String, Long>Test()
is an error. Yet when the constructor (or method) is not generic it lets you add type arguments to your hearts desire... maybe this should be considered a bug (if not in the compiler then in the specification)?– Slaw
Mar 25 at 4:51
4
4
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
Hmm... maybe not a bug. See stackoverflow.com/questions/28014853/…
– Slaw
Mar 25 at 5:03
1
1
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
@Slaw good link! this is soooo un-expected thought, the arity is different in this case, I swear I would have expected a compile time error
– Eugene
Mar 25 at 8:03
1
1
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
Great answer, thorough with links and well explained, thanks!
– Nathan Adams
2 days ago
|
show 1 more comment
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 25 at 8:19
svenmeiersvenmeier
4,8771222
4,8771222
add a comment |
add a comment |
Here:
List list = new <String, Long>ArrayList();
You have a raw type, as you don't specify a generic type for your list object on the left hand side. Providing the generic type on the right hand side is mostly not required in the first place, as the compiler can defer the generic type.
A bit of guessing here: due to fact that list itself is a raw type, the type specification given to new is somehow ignored. The other answer nicely explains why you can have type parameters in that place. But because a raw type doesn't care about type parameters, the compiler doesn't bother checking that detail here.
2
This doesn’t seem to explain that you can haveList<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even havenew <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.
– Ole V.V.
Mar 25 at 7:30
add a comment |
Here:
List list = new <String, Long>ArrayList();
You have a raw type, as you don't specify a generic type for your list object on the left hand side. Providing the generic type on the right hand side is mostly not required in the first place, as the compiler can defer the generic type.
A bit of guessing here: due to fact that list itself is a raw type, the type specification given to new is somehow ignored. The other answer nicely explains why you can have type parameters in that place. But because a raw type doesn't care about type parameters, the compiler doesn't bother checking that detail here.
2
This doesn’t seem to explain that you can haveList<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even havenew <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.
– Ole V.V.
Mar 25 at 7:30
add a comment |
Here:
List list = new <String, Long>ArrayList();
You have a raw type, as you don't specify a generic type for your list object on the left hand side. Providing the generic type on the right hand side is mostly not required in the first place, as the compiler can defer the generic type.
A bit of guessing here: due to fact that list itself is a raw type, the type specification given to new is somehow ignored. The other answer nicely explains why you can have type parameters in that place. But because a raw type doesn't care about type parameters, the compiler doesn't bother checking that detail here.
Here:
List list = new <String, Long>ArrayList();
You have a raw type, as you don't specify a generic type for your list object on the left hand side. Providing the generic type on the right hand side is mostly not required in the first place, as the compiler can defer the generic type.
A bit of guessing here: due to fact that list itself is a raw type, the type specification given to new is somehow ignored. The other answer nicely explains why you can have type parameters in that place. But because a raw type doesn't care about type parameters, the compiler doesn't bother checking that detail here.
answered Mar 25 at 4:13
GhostCatGhostCat
1
1
2
This doesn’t seem to explain that you can haveList<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even havenew <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.
– Ole V.V.
Mar 25 at 7:30
add a comment |
2
This doesn’t seem to explain that you can haveList<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even havenew <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.
– Ole V.V.
Mar 25 at 7:30
2
2
This doesn’t seem to explain that you can have
List<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even have new <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.– Ole V.V.
Mar 25 at 7:30
This doesn’t seem to explain that you can have
List<Integer> list = new <String, Long>ArrayList<Integer>();
(quoted from my answer). You can even have new <Double, Float>String("my string");
. The link provided by @Slaw seems to give good information.– Ole V.V.
Mar 25 at 7:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330697%2fwhat-do-constructor-type-arguments-mean-when-placed-before-the-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Yeah so do I, I'm not asking how to create a list lol
– Nathan Adams
Mar 25 at 2:58
4
A constructor may have type arguments that are placed there (this particular constructor hasn’t, so
<String, Long>
is just ignored). See Generics Constructor.– Ole V.V.
Mar 25 at 2:58
2
OK that makes sense, although it's weird that there's no compile error even though there's no type arguments on the constructor
– Nathan Adams
Mar 25 at 3:03
5
No error, but you get a warning about raw types. Don't use raw types. Do use the diamond operator.
List<String> list = new ArrayList<>();
– Elliott Frisch
Mar 25 at 3:05
@OleV.V. if you wanna put your comment and link as an answer I'll accept it
– Nathan Adams
Mar 25 at 3:06