aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics code mixed with legacy collection 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 "generics code mixed with legacy collection" Watch "generics code mixed with legacy collection" New topic
Author

generics code mixed with legacy collection

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

why the for loop creates ClassCastExcption for string object as Object can accept any type of
object (Intereg/String)


import java.util.*;

class Getter
{
public List getList()
{
List list = new ArrayList();
list.add(new Integer("3"));
list.add("str");
return list;
}
}
public class GetReturn {

public static void main(String... args)
{
List<Integer> list = new ArrayList<Integer>();
Getter get = new Getter();
list = get.getList();


for(Object i : list)
System.out.println(i);


}
}
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
I tried this code, and it compiles and runs successfully pritning 3 and "str".
Eleanor Leong
Greenhorn

Joined: Mar 24, 2006
Posts: 21
Hi,
When I changed
List<Integer> list = new ArrayList<Integer>();
to
List list = new ArrayList();

It runs without ClassCastException and prints 3, str.

I am confused also. Looks like the generic declaration List<Integer> causes the exception. But in fact the information of generic type should be erased after compilation.

Why the exception error!!!
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


Above code will give ClassCastException at runtime because the list in for loop returns Integer object and it also has String object in it.

Can't cast String to Integer.

Whole problem is because of the assignment list = get.getList(). get.getList() returns a raw list [IntegerObject, "str"]. You are assigning this raw list into a generic List<Integer>.

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
Hi Naseem,

As I said, I tried this code, and it comiles and runs fine with output 3 and "str".

Only when I change the FOR loop to below one do I get what you're talking about (ClassCastException)

for(Integer i : list)
System.out.println(i);

I wonder how you guys are getting exception, if you use Object in the FOR loop.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi Surendra,

Can you post the code which you executed?
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
import java.util.*;

class Getter
{
public List getList()
{
List list = new ArrayList();
list.add(new Integer(3));
list.add("str");
return list;
}
}

public class GetReturn {

public static void main(String... args)
{
List<Integer> list = new ArrayList<Integer>();
Getter get = new Getter();
list = get.getList();

for(Object i : list)
System.out.println(i);
}
}
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
I don't know why you are not getting.

Here is the output of your code:

3
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at GetReturn.main(Vct.java:22)


Naseem
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

I have gone through the K&B book and found somthing.

May be it is right of not, but i want to share it with you

As we know the generics are just compile time validations on the code, so that we do not get run time errors
like ClassCastException. Compiler removes the generic related code (parameter type from collection) and
finally code is as pre generic collection code. But in addition to this, compiler also insert somthing in
the code as i have inserted manually.

The inital program with generic when compiler will become like this as given below.


import java.util.*;

class Getter
{
public List getList()
{
List list = new ArrayList();
list.add(new Integer("3"));
list.add("i am String");
return list;
}
}
public class GetReturn {

public static void main(String... args)
{
List list = new ArrayList();// Here generic list is converted into pre generic list
Getter get = new Getter();

list = get.getList();//a pre generic list is received by a pre generic list


for(Object i : list)
{
Integer intObj = (Integer) i;/* this code is added by compiler as generic type parameter was <Inetegr>, so here ClassCastException comes. */
System.out.println(intObj);
}

}
}

Naseem, Correct me if i am wrong
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Check below code from K&B(page 577), here i just added for loop, that is creating ClassCastException. Here we are adding
one string data into integer list

import java.util.*;
class Adder
{

public void iter(List list)
{
list.add(new Integer(4));

list.add("wrong data");
}
}
public class TestBadLegacy1 {

public static void main(String... args)
{
List<Integer> list = new ArrayList<Integer>();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
Adder add = new Adder();
add.iter(list);



for(Object i:list)
{
System.out.println("integer :"+i);
}
}
}
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Can anybody SCJP 5.0 guy can correct me if my last two replies on Generics are incorrect.
Eleanor Leong
Greenhorn

Joined: Mar 24, 2006
Posts: 21
Thanks Pawan.
Its a very good answer.
I understand now.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by Pawan preet:
Can anybody SCJP 5.0 guy can correct me if my last two replies on Generics are incorrect.



They are correct. When there is cast, exception will be thrown. Question is when compiler cast.

Here I am taking three different cases for object retrieval from the list and along with I am also showing you the output from javap -c GetReturn

Case 1:



Case 2:



Case 3:



In all the above cases, object returned from the list is assigned to Object not Integer but there is a cast operation only in the third case done by compiler. (i.e., in-enhanced for-loop)

So now its clear that only enhanced for loop will return ClassCastException because of implicit cast in that case even if you assign it in Object.


Naseem
[ August 08, 2006: Message edited by: Naseem Khan ]
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Thanks Naseem
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
The following code compiles and runs by me without erors and exceptions.
Why don't I receive the same result as others do?

Guys, are you sure, that this code isn't legal?
K&B says that this code is Ok... (page 577)





[ August 09, 2006: Message edited by: Ivan Rebrov ]
[ August 09, 2006: Message edited by: Ivan Rebrov ]

SCJP 5.0 - 95%<br />Preparing for SCWCD
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
any ideas?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics code mixed with legacy collection