my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Type safty warning in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2729
    
    3

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 (Download) - the free mock exam kit for SCJP / OCPJP
HELP me! -- Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type safty warning in Generics
 
Similar Threads
How does the compiler generates the warning when we mix the generic and non-generic code?
About generics..
Generics and Warning message
Warnings during Generic assignments
What should be the o/p?