Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic quiz 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 "Generic quiz" Watch "Generic quiz" New topic
Author

Generic quiz

Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
My code below,


Which lines produce compilation warnings, and why? :roll:
[ December 18, 2006: Message edited by: Bob CHOI ]

Hard work rewards
Pinkal Patel
Ranch Hand

Joined: Jun 16, 2006
Posts: 57

Will Gave Warning

Pls find Couase in Italics format


Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
//1 # NO PROBLEM- A typed collection of type object is declared.
//2 # NO PROBLEM- A untyped collection is declared.
//3 # PROBLEM- A typed reference is being forced to refer to a collection which is not typed and can take any element... so warning for refering a possible currupted collection.
//4 # NO PROBLEM-element of type Object is added in collection of type Object.
//5 # NO PROBLEM-no collection modification.
//6 # NO PROBLEM-no collection modification.
//7 # NO PROBLEM - Unsafe collection can refer any type of collection.
//8 # PROBLEM-There is always a warning when we use a non-typed collection to add a element.
//9 # NO PROBLEM-no collection modification.
//10 # NO PROBLEM-no collection modification.


~Sanjeev Singh<br />SCJP 1.5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
more about line 4:
an object is added to an <Object> list.
Adding to a parameterized collection never produces a warning. Either it compiles (type OK) or not (type not OK).

Only reference type counts:
The variable is generic, it doesn't matter that the object (the ArrayList) is non-generic. You can safely put objects in "typeSafe". We have been already warned (that the ArrayList "typeSafe" refers to is non-generic) in line three.


Yours,
Bu.


all events occur in real time
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Warning on unchecked conversion,

- when generic collection varaible is referred to non-generic collection instance

- when non-generic collection varaible is used to invoke generic parameterized method

"simple" enough
 
Consider Paul's rocket mass heater.
 
subject: Generic quiz