File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes castNotSupportedException 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 » Java » Beginning Java
Bookmark "castNotSupportedException" Watch "castNotSupportedException" New topic
Author

castNotSupportedException

vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
Hi,
Please give me detail Explanation of this
//------------------------------------
Object o = new Object();
String s = new String("ABC");
s = (Object) s;
This kind of casting not possible it throws Exception at runtime.
//--------------------------------------
but this is possible why ?:
String s =(Object) e.nextElement();
//where nextElement return type is Object and e
//is Enumeration Instance
rom chatterjee
Ranch Hand

Joined: Dec 11, 2001
Posts: 46
Would you like to check your code as I dont see the purpose of the first line. Have you perhaps wahted to say something else. Also in the second peice of code what are you enumerating through? Let me know and Ill send you a reply straight back
romchatterjee@hotmail.com
//------------------------------------
Object o = new Object();
String s = new String("ABC");
s = (Object) s;
This kind of casting not possible it throws Exception at runtime.
//--------------------------------------
but this is possible why ?:
String s =(Object) e.nextElement();
//where nextElement return type is Object and e
//is Enumeration Instance


Rom Chatterjee<BR>Sun Certified Java Programmer
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Since you are casting, the compiler will not complain at compil time but at Runtime.
At compil time the jvm check if one reference could fit into another reference.At Runtime the JVM check if the object casted can fit into the type of your reference! There is a distinction because a type reference Object can in fact refer to a String, or anything else!
example:
Object o=new Object();
String s=new String("cool");
s=(Object) o;//this one won't work, because the type of s is String, and you are telling the compiler that you are casting into an Object, it means that you want to put an Object into a String, it couldn't fit.It's like you are doing s=o;
s=""+o;//this one work due to the implicit call to toString();
String s =(Object) e.nextElement();//this one will complain at compil time, don't know why you are saying that it works;
But this one will work
String s =(String)e.nextElement();
As long as your enumeration contains String objects!
I hope it helped you!


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
Thanx all of you now please see this

Object o = new Object();
String s = new String("Hell");
s = (String)o; //This cast is not Possible Why ?

//====================================
Properties p = System.getProperty();
Enumeration e = p.getNames();
while(e.hasMoreElement())
String s =(String) e.nextElement();//This cast is possible Why ?
System.out.println(s);
//=================================
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by vishwas bhatt:
Thanx all of you now please see this

Object o = new Object();
String s = new String("Hell");
s = (String)o; //This cast is not Possible Why ?

//====================================
Properties p = System.getProperty();
Enumeration e = p.getNames();
while(e.hasMoreElement())
String s =(String) e.nextElement();//This cast is possible Why ?
System.out.println(s);
//=================================

In the first example, the object referenced by o is not a String. It's an object. The cast that you have there, in effect, tell the compiler, I know that what o references is a String object, even though o is an Object reference. Therefore the compiler doesn't complain. However, when you try to run this, the cast is attempted and fails because what's referenced by o IS NOT a String and, therefore, can not be cast to one.
In the second exmaple, the object that is returned by e.nextElement() really IS a String object. You are returned a reference to an Object, but, once again, the cast tells the compiler that you're sure what is being returned is a String, so you want to convert it. When you execute this code, the cast occurs properly because the object returned really is a String.
The key to understanding this is to understand the difference between a compile-time type and a run-time type. Any reference variable may reference any object of its own type or any subclass of that type. Due to this, we sometimes have to perform casts.
I hope that helps,
Corey
[ March 27, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
When you do a cast you tell the compiler:" I know the reference type is different from the object type. Trust me"
And the compiler trust you!
But at runtime, the jvm checks the type of your object. In your first example at runtime, the jvm sees that the object referenced by o is really NOT a string. It is an object, and Object is not a subclass of String, so it can't fit.
In your second example, the enumeration contains Strings. So, it fit and there is no problem.
You must be able to make the difference between the reference and the Object referenced. Like Corey said:"Any reference variable may reference any object of its own type or any subclass of that type."
So an Object reference may reference,in fact, a String (or an Object, or any subclass of Object), but not the reverse!
If my explanation is not clear, let me know!
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
Thanx all of you
problem solutions is too good .and it is going to help me to.
There is One more Query regarding Enumeration Interface:
In enumeration Interface there is method
Object nextElement()
when we write e.nextElement() where e is Enumeration Reference . How it is posible to directly call abstract method on "e" where it is
Implemented.

Thanx all of u again
wating for same response
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
From the API

public interface Enumeration
An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series.
For example, to print all elements of a vector v:
for (Enumeration e = v.elements() ; e.hasMoreElements() {
System.out.println(e.nextElement());
}
Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable. Enumerations are also used to specify the input streams to a SequenceInputStream.

Strange no?
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
Hi Younes
Thnax for prompt reply...
It is too complecated for to understand this concept.
just tell in easy way if u can? otherwise give me suggestion how i can grasp this concept.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi,
Okay, suppose I have an interface MyInterface with a method f().
Suppose I have a class MyImpl which implements MyInterface, so MyImpl must implement f().
MyInterface myI = new MyInterface() is never OK, but
MyInterface myI = new MyImpl()
is OK (assuming I defined such a public constructor, of course). This is possible because MyInterface is considered to be a supertype of MyImpl. I can now invoke
myI.f()
even though myI is a reference to an interface. A reference to an interface can only contain an instance of an implementing class, so you are now free to invoke any of the interface's methods.
Now, there are methods in the util package that return an Enumeration object (remember, an interface can be a supertype), like Collections.enumeration(List l) or v.elements() where v is of type Vector, but that object must also be an instance of a class that implements the Enumeration interface, so it's okay to call hasMoreElements() or nextElement().
Does that help?
-anthony
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: castNotSupportedException
 
Similar Threads
thread q
A question about casting... .
castNotSupportedException
use of new keyword..
How to System.out.println the string reference