Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Java generics different from C++ templates? Why can't I use int as a parameter?

Tags:

java

c++

generics

I am trying to create

ArrayList<int> myList = new ArrayList<int>(); 

in Java but that does not work.

Can someone explain why int as type parameter does not work?
Using Integer class for int primitive works, but can someone explain why int is not accepted?

Java version 1.6

like image 665
yesraaj Avatar asked Jun 15 '09 13:06

yesraaj


People also ask

What is the difference between generics and templates?

Generics are generic until the types are substituted for them at runtime. Templates are specialized at compile time so they are not still parameterized types at runtime. The common language runtime specifically supports generics in MSIL.

Are C++ templates and Java generics the same?

A C++ template gets reproduced and re-compiled entirely whenever a template is instantiated with a new class. The main difference is that Java generics are encapsulated. The errors are flagged when they occur and not later when the corresponding classes are used/instantiated.

How do C++ and Java differ in their construction of generic functions?

While, C++ is platform dependent language, Java is platform independent language. The above statement is the reason why C++ is able to provide true generic types. While Java does have strict checking and hence they don't allow using generics the way C++ allows it.


2 Answers

Java generics are so different from C++ templates that I am not going to try to list the differences here. (See What are the differences between “generic” types in C++ and Java? for more details.)

In this particular case, the problem is that you cannot use primitives as generic type parameters (see JLS §4.5.1: "Type arguments may be either reference types or wildcards.").

However, due to autoboxing, you can do things like:

List<Integer> ints = new ArrayList<Integer>(); ints.add(3); // 3 is autoboxed into Integer.valueOf(3) 

So that removes some of the pain. It definitely hurts runtime efficiency, though.

like image 190
Michael Myers Avatar answered Sep 21 '22 13:09

Michael Myers


The reason that int doesn't work, is that you cannot use primitive types as generic parameters in Java.

As to your actual question, how C++ templates are different from Java generics, the answer is that they're really, really different. The languages essentially apply completely different approaches to implementing a similar end effect.

Java tends to focus on the definition of the generic. That is, the validity of the generic definition is checked by only considering the code in the generic. If parameters are not properly constrained, certain actions cannot be performed on them. The actual type it's eventually invoked with, is not considered.

C++ is the opposite. Only minimal verification is done on the template itself. It really only needs to be parsable to be considered valid. The actual correctness of the definition is done at the place in which the template is used.

like image 26
JaredPar Avatar answered Sep 19 '22 13:09

JaredPar