• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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

Now i have understood

Thanks
Anil Kumar
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic