Something is wrong with "Casting"

Discussion in 'Java' started by Patricia Klimek, Nov 30, 2004.

  1. Hey,

    the Problem I have is kind of crazy:
    I want to cast a vector to Integer and save elementAt(1) in Integer i:

    Integer i = (Integer)row.elementAt(1);

    It does show me the Element if I do this:
    System.out.println(row.elementAt(1));
    but if I cast it, it's not working.

    System.out.println(row.elementAt(1)); --- is working
    Integer id = (Integer)row.elementAt(1); --- is not working
    because of the casting
    System.out.println("id: " + row.elementAt(1)); --- is not working
    Human human = new Human(id); --- is not working


    I probably can't cast the Vector element, right?
    But how can I do it?

    I hope somebody can help me.

    Please

    Patricia
     
    Patricia Klimek, Nov 30, 2004
    #1
    1. Advertising

  2. Patricia Klimek

    KiLVaiDeN Guest

    "Patricia Klimek" <> wrote in message
    news:...
    > Hey,
    >
    > the Problem I have is kind of crazy:
    > I want to cast a vector to Integer and save elementAt(1) in Integer i:
    >
    > Integer i = (Integer)row.elementAt(1);
    >
    > It does show me the Element if I do this:
    > System.out.println(row.elementAt(1));
    > but if I cast it, it's not working.
    >
    > System.out.println(row.elementAt(1)); --- is working
    > Integer id = (Integer)row.elementAt(1); --- is not working
    > because of the casting
    > System.out.println("id: " + row.elementAt(1)); --- is not working
    > Human human = new Human(id); --- is not working
    >
    >
    > I probably can't cast the Vector element, right?
    > But how can I do it?
    >
    > I hope somebody can help me.
    >
    > Please
    >
    > Patricia


    Try Integer id = new Integer((int)row.elementAt(1));

    K
     
    KiLVaiDeN, Nov 30, 2004
    #2
    1. Advertising

  3. Patricia Klimek

    Murray Guest

    "KiLVaiDeN" <> wrote in message
    news:41ac5528$0$15070$...>
    > Try Integer id = new Integer((int)row.elementAt(1));
    >
    > K


    Vectors don't hold primitives, only objects.

    To the original poster, when you say "it doesn't work" please be more
    specific. What error are you getting? What are you putting in to the Vector?
    If it's not an Integer then you will get a ClassCastException. Show us all
    relevent code if you would like a useful response rather than a guess.
     
    Murray, Nov 30, 2004
    #3
  4. On 30 Nov 2004 03:03:05 -0800, Patricia Klimek wrote:

    > System.out.println(row.elementAt(1)); --- is working
    > Integer id = (Integer)row.elementAt(1); --- is not working


    Does 'is not working' mean the code has knocked off for the day and
    gone to the pub for a beer? Does it mean the code is simply lazy?
    <http://www.physci.org/codes/javafaq.jsp#exact>

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Nov 30, 2004
    #4
  5. Patricia Klimek

    MaSTeR Guest

    "Patricia Klimek" <> wrote in message
    news:...
    > Hey,
    >
    > the Problem I have is kind of crazy:
    > I want to cast a vector to Integer and save elementAt(1) in Integer i:
    >
    > Integer i = (Integer)row.elementAt(1);
    >
    > It does show me the Element if I do this:
    > System.out.println(row.elementAt(1));
    > but if I cast it, it's not working.
    >
    > System.out.println(row.elementAt(1)); --- is working
    > Integer id = (Integer)row.elementAt(1); --- is not working
    > because of the casting
    > System.out.println("id: " + row.elementAt(1)); --- is not working
    > Human human = new Human(id); --- is not working
    >
    >
    > I probably can't cast the Vector element, right?
    > But how can I do it?
    >
    > I hope somebody can help me.
    >
    > Please
    >
    > Patricia


    You do know that any collection in Java is zero based ?
     
    MaSTeR, Nov 30, 2004
    #5
  6. Patricia Klimek wrote:

    > the Problem I have is kind of crazy:
    > I want to cast a vector to Integer and save elementAt(1) in Integer i:


    You cannot cast a Vector to Integer. It isn't one. If some element of
    the vector is an Integer then you can cast that; that is what your first
    line of code below attempts to do:

    > Integer i = (Integer)row.elementAt(1);


    The method invocation operator (.) has precedence over the typecast, so
    it happens first and you are attempting to cast the return value from
    type Object to type Integer. That will succeed of the element is an
    Integer or if it is null.

    > It does show me the Element if I do this:
    > System.out.println(row.elementAt(1));
    > but if I cast it, it's not working.


    The only reason a cast ever fails is that the class of the object being
    cast is incompatible with the type it is being cast to. In that case a
    ClassCastException is thrown. If you are not getting a
    ClassCastException then the cast is not your problem; if you _are_
    getting one then the problem is that the object you are trying to cast
    is not an Integer.

    Do make sure you understand that Java typecasts do not *change* the
    class of an object -- they simply declare it to be a certain type (or a
    subtype) in a way that can be checked at runtime. That means, for
    instance, that no String can be successfully cast to Integer, even one
    that contains a representation of a number (e.g. "42"). For that
    particular case you need to create an Integer by means of
    Integer.valueOf(String), or possibly to obtain an int (primitive) value
    by means of Integer.parseInt(String).


    John Bollinger
     
    John C. Bollinger, Nov 30, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Helen
    Replies:
    2
    Views:
    354
    Scott G.
    Jul 12, 2004
  2. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  3. Guest
    Replies:
    4
    Views:
    497
    Guest
    Oct 13, 2004
  4. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    577
    James Kanze
    Dec 20, 2009
Loading...

Share This Page