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 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 "Generics" Watch "Generics" New topic
Author

Generics

anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

import java.util.*;

class collection
{
public static void main(String... args)
{
List l=new ArrayList();
l.add("A");
l.add("B");
l.add("C");
Iterator i=l.iterator();
while(i.hasNext())
{
System.out.println("The value is : "+i.next());//line 1
}
}
}


In the above program why i am not getting compile time error at line 1 .

Here i did not declare the Iterator at generic type.So The Object comming is of type Object.And without type casting i am trying to access the value.
So here i have to get compile type error.But it is not like that.Why ?

Please Explain this

Thanks
Anil Kumar
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Why would you need to cast, raw iterator reference type i's next() method will return Object, and because in fact at run time the type will be String and String class defines the toString() method the actual content of the
object is printed.

And yeah what I called run time type, try the following line of code:
while(i.hasNext()){
System.out.println(i.next().getClass());
}
Output:
The value is : class java.lang.String
The value is : class java.lang.String
The value is : class java.lang.String


cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi
Chandra

Replace the while with this look.
And here it is sure that we will get compile time error.Because the return type is Object and we are assigning to String.
Here it is true.


/*
while(i.hasNext())
{
String s=i.next();

System.out.println("The value is : "+s);
}*/

while(i.hasNext())
{System.out.println("The value is : "+i.next());//line 1}

But here at line 1 the comiple time constant is String
ie
"The value is : "+
Before + it is String and after + if we have String then it is a compile time constant.But here the return type of i.next is Object.And the compiler even knows about that.

That is why i am not getting.

Thanks
Anil Kumar
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Did you cast Object to String anywhere in your second code.
No! You are simply concatenating the result i.next() to the String "The value is ". That's it!

What is simply happening there: i.next() returns the object and because the runtime type of that is String so the overridden version of the toString() will be called from the String class to show you the result.

ClassCastException occurs when there is something runtime incompatible assignment is done.

Object o1 = new Object();
String s1 = (String)o1; //CCE will be thrown

Object o2 = new String();
String s2 = (String)o2; //NO PROBLEM AT RUNTIME, because the runtime type of the o2 will be String so assigning String object to String ref variable is no sin.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Chandra
---------------------------------------------------------------------
ClassCastException occurs when there is something runtime incompatible assignment is done.
---------------------------------------------------------------------

Now i have understood

Thanks
Anil Kumar
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi,

you will get compile time error when you write

String aa=i.next(); // error

System.out.println("The value is : "+aa);//line 1

Thanks,

Abdul Mohsin


Regards, Abdul Mohsin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics