• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type safty warning in Generics

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic