Question

Programming language is Java. Why can't we do something like the following? public foo(Consumer consumer) {...

Programming language is Java.

Why can't we do something like the following?

public foo(Consumer consumer) {
    Type type = consumer.getGenericTypeParameter();
}

Homework Answers

Answer #1

public foo(Consumer consumer) {

    Type type = consumer.getGenericTypeParameter();
}

Generic type information is only available to the compiler, not the JVM. In other words,  generic type information is not available to the JVM at runtime, only compile time. This is called type erasure.

The above example is a pseudo code equivalent of what things might look like without type erasure , but unfortunately, it is impossible. Once again, the generic type information is not available at runtime.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
   QUESTION 6 1.   If we wanted something like the python else statement for a counter...
   QUESTION 6 1.   If we wanted something like the python else statement for a counter controlled loop in Java, what would it look like?        Since there is no way for a loop in Java to tell you if it completed without breaks or continues, you'd need to set a boolean flag to indicate that and check it after the loop was completed.        We simply check either break or continue flags to see if either is...
What does the following application print and explain why it happens? public class Foo {            ...
What does the following application print and explain why it happens? public class Foo {             public void crazyArray(int[] vals){                   int[] newVals = { 1, 7, 6, 1, 3 };                   vals[0] -= vals[0] - newVals[3];                   vals[3] = vals[2] + vals[1] - newVals[4];                   vals[2] -= vals[0] *2;                   vals = newVals;                   vals[1] -= vals[2];                   newVals[2] *= 3 + vals[3];                   vals[1] -= 14 + vals[2];             }                         public static void main(String[] args) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} } Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate. Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer. Your call will...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add new instance variables or modify existing ones. private LinkedNode<T> head; private LinkedNode<T> tail; private int size; Q1: write a method called "public T removeFirst()" that does the following: * Removes and returns the first element of the deque. * Must be in O(1) * @return the data formerly located at the front of the deque * @throws java.util.NoSuchElementException if the deque is empty Q2:...
By JAVA programming language public void expand(Object a) {     // assume element has enough capacity...
By JAVA programming language public void expand(Object a) {     // assume element has enough capacity     for (int i = size - 1; i >= 0; i--) {         element[4 * i + 3] = a;         element[4 * i + 2] = a;         element[4 * i + 1] = a;         element[4 * i] = element[i];      }      size = 4 * size; } element is a one-dimensional array that stores elements of the type Object....
QUESTION 1 1.   Which programming languages do not have iteration statements and why?        Imperative...
QUESTION 1 1.   Which programming languages do not have iteration statements and why?        Imperative programming languages require recursion to do anything iteratively        Object-Oriented Programming languages do not use iteration since functions operate over data structures.        Functional programming languages since they do not use variables and counting loops require variables.        Functional programming languages do not have variables and iteration requires variables to operate. QUESTION 2 1.   One of the downsides of using expressions...
Explain why paying someone to do something they like doing reduces their intrinsic motivation?
Explain why paying someone to do something they like doing reduces their intrinsic motivation?
1.) Why is it that the more we do something, the better we get it but...
1.) Why is it that the more we do something, the better we get it but we also receive diminishing returns? How can these two things happen? Is there a conflict? 2.) Why do consumers make a choice by looking at both the Marginal Utility (MU) and the price for a product (MU/P) rather than just the price or just the marginal utility? Explain
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...