Results 1 to 5 of 5
  1. #1
    StanO is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Array constructors

    Hello,

    I have a beginner Java question about array constructors.

    This is okay:

    final int[] blah = { 45, -45 };
    for (int i : blah ) { ; }

    This is illegal but would be sweet:

    for (int i : { 45, -45 } ) { ; }

    This works but is ugly and greater space/time?

    for (int i : new int[] { 45, -45 } ) { ; }

    Comments?

    TIA!
    StanO

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by StanO View Post
    Comments?
    Yup, should { 45, -45 } be an int array or a byte array or a short array or what? The compiler can't decide that for you.

    kind regards,

    Jos

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by StanO View Post
    This works but is ugly and greater space/time?

    for (int i : new int[] { 45, -45 } ) { ; }
    I don't see how it's greater space/time. Either way, the VM allocates an array of two Integers. Whether or not that's explicitly declared has no effect on how much space or time it takes to create the array. (Also agree with what Jos said.)

  4. #4
    StanO is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    You're sure that the compiler will create that array object just once and reuse it the next time the code that includes the loop is called?

    If it gets created each time the loop is called, then it is not-only ugly...

    Clearly, the first case will only get constructed once.

    Thanks again!

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by StanO View Post
    You're sure that the compiler will create that array object just once and reuse it the next time the code that includes the loop is called?

    If it gets created each time the loop is called, then it is not-only ugly...
    It will only create it once, but what I'm saying is that it has no advantage or disadvantage over just doing {45,-45} (if it were legal) in the first place. Both have to allocate the same amount of data at the same time and for the same purpose.

Similar Threads

  1. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM
  2. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM
  3. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  5. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM

Posting Permissions

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