Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

vector

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
when i worked the following code
1//class vector1
2//{
3// public static void main(String a[])
4// {
5// Vector v = new Vector();
6// v.addElement(new Integer(1));
7// v.addElement(new Double(34.44));
8// v.addElement(new Float(2.3));
9// System.out.println("1st element"+ v.firstElement());
10// System.out.println("last element"+ v.lasttElement());
11// }
12// }
The above works fine.But if i replace line10 with
System.out.println("last element"+(Integer) v.lastElement());
I get ClassCastException at Runtime.Can someone explain
me why is it so?


------------------
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avn,
You are not using cast correctly. Try this code that first cast for Float, remember that lastElement() returns an Objetc, and after, converts this value for int using intValue method.
import java.util.Vector;
class Vector1{
public static void main(String a[]) {
Vector v = new Vector();
v.addElement(new Integer(1));
v.addElement(new Double(34.44));
v.addElement(new Float(2.3));
System.out.println("1st element "+ v.firstElement());
System.out.println("last element "+ ((Float)v.lastElement()).intValue());
}
}
If you do not understand, tell me what is not well explained and I shall try to do it better.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adrian Ferreira:
Avn,
You are not using cast correctly. Try this code that first cast for Float, remember that lastElement() returns an Objetc, and after, converts this value for int using intValue method.
import java.util.Vector;
class Vector1{
public static void main(String a[]) {
Vector v = new Vector();
v.addElement(new Integer(1));
v.addElement(new Double(34.44));
v.addElement(new Float(2.3));
System.out.println("1st element "+ v.firstElement());
System.out.println("last element "+ ((Float)v.lastElement()).intValue());
}
}
If you do not understand, tell me what is not well explained and I shall try to do it better.


Thanks for ur reply Andrian.But i am not clear with it.
Do u mean to say that a Float Object value cannot be directly
typecasted using Integer.Then it shouldnt it be
((Integer)v.lastElement()).intValue().

------------------

[This message has been edited by avn (edited June 27, 2000).]
 
Adrian Ferreira
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avn,
Take a look at Vector API, you would see that lastElement returns an Object, so one clear use for this method would be:

In this case you cast an Object type to Float type. The compiler does not complain because Float "is_a" Object. At run time, there is no problem because you did put an Float object into Vector. You know this but the compiler doesn�t, so you need cast as (Float).


Thanks for ur reply Andrian.But i am not clear with it. Do u mean to say that a Float Object value cannot be directly typecasted using Integer.Then it shouldnt it be


See that here you are casting an Object, that is returned from lastElement(), to Integer. So, there should be no problem at compile time but it doesn�t run well since a Float is not ("is_a"" is not valid) an Integer object. To be short, type is checked at compile time but in Java, each object knows what class it is an "instanceof" and it is checked at run time.
Hope to help you.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adrian Ferreira:
[B]Avn,
Take a look at Vector API, you would see that lastElement returns an Object, so one clear use for this method would be:

In this case you cast an Object type to Float type. The compiler does not complain because Float "is_a" Object. At run time, there is no problem because you did put an Float object into Vector. You know this but the compiler doesn�t, so you need cast as (Float).


See that here you are casting an Object, that is returned from lastElement(), to Integer. So, there should be no problem at compile time but it doesn�t run well since a Float is not ("is_a"" is not valid) an Integer object. To be short, type is checked at compile time but in Java, each object knows what class it is an "instanceof" and it is checked at run time.
Hope to help you.[/B]
</BLOCKQUOTE>
Thanks for the explanation.Now,i am clear.

------------------
 
please buy this thing and then I get a fat cut of the action:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic