type erasure and wild card oprator reference to java generics

Hi,

I would like to  know about 'type erasure(why happens at runtime not at compil;e time) and wild card oprator reference to java generics'.

Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Type erasure is a bit of a complex idea.  It's basically a rather unfortunate side effect of the way generics are implemented in Java.

It's pretty obvious that when you have:
List<Integer> myIntegerList ;
it's not the same as
List<String> myStringList ;

And indeed the compiler will help enforce this.  So things like:
String str = myIntegerList.get(0) ;
won't compile.

That's compile time checking.

But at runtime, Java loses this information.  So you can do naughty things like:
List myList = myIntegerList ;
myList.add("Hello") ;  // this works but it'snot good - we add a String to a list of integers

This code works, but we'd rather it didn't.  It works because of "type erasure" - which means at runtime the List has no idea what type of objects can be stored in it - so you can add any object to the list and it succeeds - even though initially you declared it as a List of integers.

So why does type erasure happen?

It's to do with the fact that Java first was written without generic types.  So all Lists were Lists of Objects - no way to say "this is a list of Integers".  When they added them, this was the only way to make the two kinds of lists work together - the old ones and the new ones (with generic types).

So basically it's a rather ugly compromise.  Ideally Java would have started life with Generics and the type checking would have been enforced at both compile time and runtime.

Here's a piece of code to show all of this in action - as we turn an Integer list into something that actually contains an Integer and a String:

	public static void main(String[] args) {
		List<Integer> myIntegerList = new ArrayList<>() ;
		myIntegerList.add(7);
		List myList = myIntegerList ;
		myList.add("Hello") ;
		System.out.println("My list " + myList) ;
	}

Open in new window


I think wild card operators are a totally separate question.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
it is strange at runtime type check is compromised
gudii9Author Commented:
import java.util.ArrayList;
import java.util.List;


public class TypeErasure {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main(String[] args) {
			List<Integer> myIntegerList = new ArrayList<>() ;
			myIntegerList.add(7);
			List myList = myIntegerList ;
			myList.add("Hello") ;
			System.out.println("My list " + myList) ;
		}

	}

Open in new window


when i run i got corrupted output with string
gudii9Author Commented:
List<Integer> myIntegerList = new ArrayList<>() ;
how above different from below
List<Integer> myIntegerList = new ArrayList<Integer>() ;


I am hoping both same?
dpearsonCommented:
List<Integer> myIntegerList = new ArrayList<>() ;

how above different from below
List<Integer> myIntegerList = new ArrayList<Integer>() ;

They are exactly the same.  I just used the shorter form - which is now legal in Java.  You're using the longer form (where you put Integer on the right hand side as well).

Doug
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.