I am doing a problem which involves understanding the differences: The class Vec
ID: 3550815 • Letter: I
Question
I am doing a problem which involves understanding the differences:
The class Vector cannot be instantiated for any arbitrary type. For example, consider the following instantiation for a wrapper integer class.
Explain why the second implementation fails. What must be added to that class so this program will compile? Suppose this program were written in Java. Explain how Java allows the constraints on a generic type parameter to be specified and how they would be specified in this case
Java does have one limitation, however. Although wrapper classes can be used to instantiate generic type parameters, primitive types cannot. Explain why.
Explanation / Answer
The problem with the Int class is that it doesn't implement the stream operator (<<). For Vector to be instantiated, the type of instantiation needs to have this operator implemented or the put() function won't compile.
Java solves that by having standard toString() methods that are implemented in every object giving it the ability to convert any object to a string representation, much like the C++ stream operator (<<) does. Unfortunately, Java's primitive types aren't objects so they do not implement toString().
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.