Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type safty warning in Generics

 
suresh pilakal babu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
class Gen68
{
public static void main(String[] args)
{
ArrayList arl = new ArrayList();
//arl.add("test"); --------line #1
}
}

Why the compiler is not warning us about the typw safety here?
It warns only when we add something to arraylist(--------line #1)
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you add something to a collection the compiler sees no need to warn you because there are no chances of mixing your types. Isn't it?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4682
332
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Harvinder Thakur:
Unless you add something to a collection the compiler sees no need to warn you because there are no chances of mixing your types. Isn't it?


Yes! And... The compiler can't warn you unless you used Generics there. You didn't use Generics here, so it is same as to writing like this:

In this case, you CAN put 'any' object to this array list. So no need to worry about the type safety here.

Devaka
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic