Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

Generics in java

Generics: This happens at compile-time. The compiler checks for the type correctness of the program and translates or rewrites the code that uses generics into non-generic code that can be executed in the current JVM. This technique is known as “type erasure”. In other words, the compiler erases all generic type information contained within the angle brackets to achieve backward compatibility with JRE 1.4.0 or earlier editions.

I have not understood clearly what it means.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
gudii9
Asked:
gudii9
  • 4
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
This is best explained with a simple example. Say you have a List defined using generics as such...     List<String> stringList;      This basically intends to mean a List that can only hold objects of the String type. However, this constraint is only enforced at compile time, ie. it is the compiler that gives an error if you try to do something like...    stringList.add(new Integer(1));    It will say that you can't put an object of type Integer into a List with generic type String.

The point to this is that when it comes to runtime, there is no constraint enforced that objects in this List have to be Strings, ie. the fact that the List has a generic type of String has been "erased". If you can get around the compiler enforcing the List to only contain Strings, you can still put any object in the List. Consider the following code...
import java.util.ArrayList;
import java.util.List;

public class TestGenerics {
    
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<String>();
        
        stringList.add("Foo");
        stringList.add("Bar");
        
        ((List) stringList).add(new Integer(1));
        
        for (Object item : stringList) {
            System.out.println("Item: " + item);
        }
    }
    
}

Open in new window

The above code will compile perfectly fine (although with a warning about us trying to trick the compiler) producing a .class file that can be executed by the JVM. Even when we do execute the code, it will run perfectly fine, ie. it will execute line 12 with no problems and so the List at this point contains 2 Strings and 1 Integer. The for loop even works printing the 2 Strings and the Integer out to the console.

So we have proven that our List<String> can actually hold non-String objects at runtime.
0
 
gudii9Author Commented:
Is it is a bug which needs to be resolved in later releases of java? please advise
0
 
gudii9Author Commented:
((List) stringList).add(new Integer(1));

How the above line compiled.


If you can get around the compiler enforcing the List to only contain Strings, you can still put any object in the List.

How did we get around compiler enforcement.
I do got output as below
Item: Foo
Item: Bar
Item: 1


Please advise
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
((List) stringList).add(new Integer(1));

How the above line compiled.
Basically, it is saying to take "stringList" which was originally defined as "List<String>" but cast it to be a plain "List". And then, because it is (temporarily) a plain List object, then we can call the .add() method and pass an Integer object.

How did we get around compiler enforcement.
Exactly by what I just said above. Normally, stringList is a "List<String>" and so the compiler ensures that we only add String objects. But by casting it as a plain "List", we can now add ANY type of object to it.

I do got output as below
Item: Foo
Item: Bar
Item: 1
That is correct. It is showing you that the list contains both Strings and Integers.
0
 
gudii9Author Commented:
I see the trick.

((List) stringList).add(new Integer(1));
       


can i cast instead to List of some other type other than String say custom object CUSTOMER like

((List<CUSTOMER>) stringList).add(new Integer(1));
       
Please advise
0
 
gudii9Author Commented:
Please advise
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now