Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Nithya is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default How to convert Integer[] to int[]

    Hi, I want to convert an Integer[] to int[]. How can I do it?

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

    Default

    First of all, please choose the correct sub-forum next time posting here. Read our FAQ page as well.

    Go through the Integer array and copy each element to the int array. Did you try that?

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    int x;
    Integer s=12;
    x=(int)s;
    System.out.print(x);

    cut and paste above code in your main method of ur java class,and run the application.
    i hope u will be clear by the code,

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

    Default

    Quote Originally Posted by makpandian
    Java Code:
    int x;
    Integer s = 12;
    x = (int)s;
    System.out.println(x);
    REALLY DOES THIS WORK??? I don't think so... How can the compiler convert between an Integer object and a primitive type? Anyway this would be a bad practice, even if it worked

    Quote Originally Posted by Eranga
    Go through the Integer array and copy each element to the int array
    That is the right way

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Integer is a "wrapper" around the primitive type. That means it holds the primitive type as a field. Look at the API listing for Integer for methods that explicitly return the primitive type. The method mentioned above also works, since the compiler recognizes that Integer is a wrapper for int.

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

    Default

    Yes, the way makpandian pointing is working. But it's not good practice at all. Why you need an explicit casting there, useless and very bad logic it is for me. Casting an object into a primitive is the case.

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

    Default

    I have to quote myself:
    Quote Originally Posted by raffaele181188
    REALLY DOES THIS WORK??? I don't think so...
    I tried and I realized it works. But I think that is NOT the right way to solve his problem. First of all he wants to convert between arrays, not just single instances...

    And the cast is very unpleasant to read. With generic types there's no real need to use casts in Java nowdays. Maybe there's still some exception, but I think you seldom need to cast. And this is not the case, you MUST use the intValue() method, and copy each element in the array using a for loop

  8. #8
    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
    How can the compiler convert between an Integer object and a primitive type? Anyway this would be a bad practice, even if it worked
    Its called autoboxing and was added in Java 1.5

    One could argue that having int, float, and other non-object types (all of which are cruft left over from C) is bad, but they are part of Java. Get used to it.

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

    Default

    Quote Originally Posted by fishtoprecords
    Its called autoboxing and was added in Java 1.5
    You're right. I know that story but I had never seen an explicit cast between the primitive value and its object counterpart. I believed autoboxing to be a kind of "compiler's magic" introduced to help young and unexperienced developers. And I still think so, since mixing ints and Ingtegers leads to "mystic" code. I always use:
    Java Code:
    Integer i = new Integer(anIntVariable);
    ....
    int anInt = i.intValue();
    And there's no real need in this situation to use such a nasty cast.
    IMHO

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

    Default

    I don't see any "nasty cast" in either option. Or no real difference between them

    Java Code:
    int an1Int = i.intValue();
    int an2Int = (int) i;
    There are fewer characters in the second version.

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

    Default

    Yes, you save 5 characters. But why USING CASTS? If you have a "regular" OO way to do that, simply invoking a method...

    I have to repeat, nowdays there's no real need to use casts in Java code... At least in simple exercises...

    It's a C practice to convert from a type to another one with a cast.

  12. #12
    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
    It's a C practice to convert from a type to another one with a cast.
    Well, you seem to have a theological argument with them, and I can't argue theology.

    casts are needed occasionally, not as a general practice, but sometimes. More often with the left-over C constructs of int, float, double, byte, etc. rather than pure OO flavors of Integer, Float, etc.... but the language includes the crocks from C, and they get used every day in every program.

    There are other things to worry about, IMHO

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

    Default

    @fish
    It' not a theological argument... It's simply bad programming practice, it's about elegance. Why using casts (a language fature) when you have a method (a programming feature) that does the same thing? It's simply useless in this case.

    Java creators introduced generics to avoid casts. And I think now there's no real need to cast anything.. Can you make me an example in which you are forced to cast (eg there are no other ways) between Objects (not primitives)?

  14. #14
    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
    It' not a theological argument... It's simply bad programming practice, it's about elegance.
    By your religion, its bad practice. Not by mine.

    I learned long ago that the way to peace is to not argue religion.

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

    Default

    I learned long ago that the way to peace is to not argue religion
    You're right, but there are times when you need a meter, a way to judge something. For example if a child said:
    30 = 5 + 5 + 5 + 5 + 5 + 5
    You would:
    "Hey, that is not the right way! 30 = 5 x 6"
    However both work... But the second is truly the best approach! In math sciences elegance has always been considered an important quality, and programming is a math application. Casts were needed when Java was created, nowdays there's no need. If you can:
    Quote Originally Posted by myself
    Can you make me an example in which you are forced to cast (eg there are no other ways) between Objects (not primitives)?
    If you teach someone the right way to do something then you prevent him from making mistakes (and casts between objects are error-prone) and a cast is not needed in this case

  16. #16
    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
    For example if a child said:
    30 = 5 + 5 + 5 + 5 + 5 + 5
    You would:
    "Hey, that is not the right way! 30 = 5 x 6"
    However both work...
    No, here you are wrong. They are both correct.
    The second is simply a collapse of the first, its syntactic sugar.

    The two examples are exactly the same:

    30 = 5 + 5 + 5 + 5 + 5 + 5 = 5 x 6

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

    Default

    YES THEY ARE THE SAME!!! :D
    The thing is, in 2009 no one has to do 5 + 5 + ..., everyone should 5 x 6

    Casts are the same. You will always be able to cast but why doing it, since the Java creators too felt casts are dangerous?

    In this case there is no need to cast. And if you can make me that example I'll always be waiting for you

  18. #18
    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
    Casts are the same. You will always be able to cast but why doing it, since the Java creators too felt casts are dangerous?
    Do you have a citation for this claim?

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

    Default

    Yes I have. Official Sun Tutorial:
    Generics
    Sun introduced generics, there MUST be a reason... Have you found your example to demonstrate the need for casts?

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

    Default

    Sun introduced Generics in Java 1.5. There is lots of code that was written before then. And generics are not cleanly backwards compatible, so that old code is not going away anytime soon.

    Plus, generics are just compile time, they do nothing at run time.

    You must cast every time you override equals. While there are many ways to do it, I generally do it something like:

    Java Code:
    public boolean equals(Object obj) {
       if (obj instanceof SomeClass) {
             SomeClass anSC = (SomeClass) obj;
             return equals(anSC);
        } else return false;
    }
    public boolean equals(SomeClass arg) {
      // real implementation
    }

Page 1 of 2 12 LastLast

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
  •