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 List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "List" Watch "List" New topic
Author

List

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
List<Integer> ls1 = new LinkedList(); //line 1
List<String> ls2 = new LinkedList(); //line 2
List ls3 = new LinkedList(); //line 3
List ls4 =ls1; //line 4
List ls5 =ls2; //line 5
List ls6=ls5=ls4; //line 6


which line(s) contains error?


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
Ed Thompson
Ranch Hand

Joined: Jan 20, 2006
Posts: 43
List<Integer> ls1 = new LinkedList<Integer>(); //line 1
List<String> ls2 = new LinkedList<String>(); //line 2


Even if the voices <i>aren't</i> real, they still have some good ideas!
Sujatha Musunuri
Greenhorn

Joined: Oct 23, 2007
Posts: 16
Hey Ed Thompson,

Do U say that the lines 1 & 2 give error? Can U let me know the cause?
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
i believe both curly bracket on ref and obj sides have to be the same.

but what the, what about line 6???
is it even legal?
Ed Thompson
Ranch Hand

Joined: Jan 20, 2006
Posts: 43
I just plugged it into a class in Eclipse and let it complain.

The warning I got was a Type Safety warning:
"Type safety: The expression of type LinkedList needs unchecked conversion to conform to List<Integer>"

Beyond that I got no errors.

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy,

all lines compile fine.

Only 1 and 2 will compile with a warning. Because you assigned a generic variable to a non-generic object. Allowed, compiles nicely, but you get a warning.


Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List
 
Similar Threads
Generic Question in JAVA Beat
Whether code will compile or not..??
questions on generics
Generic Assignments
some questions from generics