Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Quote Originally Posted by fishtoprecords
    You must cast every time you override equals()
    Yes, you're right. :D And this is NOT a special task, it's quite common and I can't figure out why they didn't redisign the signature to use generics. I think it wouldn't have been such a difficult thing. Anyway Java 1.5 has been around for 3 years, so I think it is not a new thing... And generics are only a compile-time feature, so the code is perfectly backwords compatible (this is what I know, but if you have more informatin :) ...)

    Above all Nithya (the original author of this post) is a STUDENT. And I think that introducing features like autoboxing or casting between Objs and prims to him is very premature and could confuse him.

    I think that casts are generally a bad thing and you should avoid it. In this case (and lots of other cases) there is no need to cast, so there is no need to defend them

  2. #22
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by raffaele181188 View Post
    I can't figure out why they didn't redisign the signature to use generics. I think it wouldn't have been such a difficult thing.
    One major design rule for new Java versions is to keep backward compatibility. They could not change the signature without breaking 7 years of code.

  3. #23
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Yes backward compatibility is an important issue but I mean, for example:
    //This is pseudo-code... I don't know if it even compiles...
    public class Object<T> {
    ...
    public boolean equals(? extends T);
    ...
    }
    Is it possible using generics? I can only use generic code but actually I have never written generic types or methods...
    Generic methods

    They have rewritten the Class class (which now is Class<T>, and Comparable etc...) without breaking back-comp, it seems... Do you have further info? Moreover this would be an important feature, because it makes no sense to test an Integer and a JButton for equality :p ...

  4. #24
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Hey, this looks like fun!

    I remeber when I was sat at a piano by my grandmother. She told me in effect that all the ineffect ual intellecutals could correctly code all the code that could be coded, generic ( ally ) Pat can spell, I cannot. Can Cats dance? Who says Cats can't dance?

    Generics may be good for Geriatrics, without knowing the code behind them, you get behind. Every time you try to get the code behind them, Dogs in Blind Alleys take you to the C-Way. In Java, there is no void anything anywhere, that's what Generics sounds like to me in practice. Instead of using Generics, why not get some editors that will show the code?

    Without a full-blown codebase, the question "test and Integer and a JButton for equality" is an indeterminate.

    Therefore appropriate for a razz, and I take your challenge:

    One cannot back-comp on a codebase that is not even semi-portably consistent across all problem domains written to run on all platforms.

    At some point, a 32/64 bit pointer gets ripped to a call into 16-bit single threaded runtime lib,....

    The result is rampant downloads, choking the internet to it's knees.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #25
    awilson is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default Missing the question...

    The original question was how to cast an array of objects to an array of scalars, more specifically Integer[] -> int[].


    Integer[] objArr = ...
    ...

    // create a new scalar int array the same size as the Integer array
    int[] recs = new int[objArr.size()];

    // copy the data from the Integer array to the int array
    for (int idx=0; idx<objArr.length; idx++) {
    recs[idx]=objArr[idx].intValue();
    }


    enjoy...

  6. #26
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Although I risk reanimating this thread: why are you using .intValue()? As the very first answer asked, "Go through the Integer array and copy each element to the int array. Did you try that?"

    The only time it will fail is if the original array contains a null element. And it should fail in that case.

  7. #27
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 01:07 PM
  2. Integer length
    By jithan in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:35 PM
  3. Replies: 0
    Last Post: 04-06-2008, 08:40 PM
  4. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:39 PM
  5. Integer vs int
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-14-2007, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •