Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does this not compile?

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


am i right that the variable T cant be used at runtime? that is why i get a compile error here?

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess so..you have only 2 days left for exam dont confuse yourself, you can explore things after the exam to get confused.

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this will help you a little bit.
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this has to do with type erasure...
I don't know about type erasure exactly but will give a simple overview.

please google about type erasure to understand it better( but after the exam okay)

All the best
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raju,

I think, your question is why the first one gives the type erasure related error while the first one does not. The answer is that when you compile the second code ie..



compiler generates the corresponding bite code as follows -




In this case, you can see that all the type information was erased and T was converted to Object.... Now think of the first code mentioned by you.. ie



In this case, you are using instance of operator on T. Java compiler did not give this as an option and declared this an error due to obvious reasons that converting (my instanceof T) to (my instanceof Object) after compilation would have confused many developers as they could have been doing it thinking a type safe operation but it wold have not been.

I hope this would clear all you confusion.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks salil....upto the point explaination.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic