File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Type safty warning in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Type safty warning in Generics" Watch "Type safty warning in Generics" New topic
Author

Type safty warning in Generics

suresh pilakal babu
Ranch Hand

Joined: Jul 01, 2008
Posts: 31
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

Joined: Jun 10, 2008
Posts: 231
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?


thanks
Harvinder
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3268
    
  52

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


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type safty warning in Generics