Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Converting Object to Long

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Converting Object to Long

    How can I convert an Object to a Long?

    I have a method that returns an ArrayList of either Integer Objects or Long Objects, so it returns just an ArrayList of Objects. I need to convert those Objects to Longs so I can use them. I've tried parsing and casting, but both cause compiler errors.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Converting Object to Long

    Could you post your code? It's kind of hard to figure out what's going on without it.

    Does the ArrayList hold longs and integers at the same time, or is it either all longs or all integers?

    It sounds like reflection or wild-cards could help you solve this problem (or even just refactoring your code)

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Converting Object to Long

    Or use instanceof before casting.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Converting Object to Long

    The method returned either an ArrayList<Integer> or an ArrayList<Long>, depending on what was given to it. So since it could return either, it returned ArrayList<Object>. I needed to cast those Objects, regardless of Integer or Long as Longs. I figured out I could do this by saying:
    Long.parseLong(arr.get(y).toString());

    I was being stupid and tried to send the Long.parseLong method the object. After looking at the API, I realized I just had to toString it...

    I'm good now.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Converting Object to Long

    Rather than saying it returns a ArrayList of objects, use the wild generic qualifier.

    public ArrayList<?> myMethod()
    {
        // for ArrayList of longs:
        return new ArrayList<Long>();
        // for ArrayList of int's:
        return new ArrayList<Integer>();
    }

    Better yet, you can give it qualifiers for what it is going to return via polymorphism

    public ArrayList<? extends Number> theMethod()
    {
        // code. Return statements are the same as above
    }

    This allows you to actually return an ArrayList<Long> rather than an ArrayList<Object>.
    Last edited by helloworld922; August 19th, 2010 at 09:53 PM.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Converting Object to Long

    I like that way. I'll have to use that in the future.

Similar Threads

  1. Writing long variables to file
    By cuthbertmd1 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 15th, 2010, 09:08 PM
  2. Sudoku Creator (this could be a long one)
    By aussiemcgr in forum Java Theory & Questions
    Replies: 8
    Last Post: July 27th, 2010, 12:19 PM
  3. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  4. How long did it take you to learn?
    By JavaLearner in forum The Cafe
    Replies: 5
    Last Post: March 24th, 2010, 04:42 PM
  5. The input line is too long
    By vivekmk in forum Java IDEs
    Replies: 5
    Last Post: October 16th, 2009, 10:35 PM