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: Array definitions

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array definitions

    What is the difference between following 2 approaches in creating an array.
    Which is preferred depending on the use?

    Approach 1:


    int a[]= new int[5];
    a[0] = 98;
    a[1] = 97;
    a[2] = 99;
    a[3] = 94;
    a[4] = 96;

    Approach 2:
    int a[]={98,97,99,94,96};

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default re: Array definitions

    Also posted and answered at: https://coderanch.com/t/703504/java/array-java
    and https://www.dreamincode.net/forums/t...array-in-java/

    Please use more descriptive title for your posts. All questions on this forum are about Java.
    I've changed this one. Next time be sure to use a better title.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Array definitions

    It doesn't really matter. I prefer the 2nd approach because there is less typing and I'm less likely to make a mistake. But the compiler generates the same byte code for both.

    Regards,
    Jim

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array definitions

    One issue might be where the code is located. The first would need to be inside a method. The second could be outside of a method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array definitions

    Quote Originally Posted by jim829 View Post
    It doesn't really matter. I prefer the 2nd approach because there is less typing and I'm less likely to make a mistake. But the compiler generates the same byte code for both.
    If we're to simply fill the thing with one constant after another, devoid of any other kind of complicated construction, the second approach is dramatically preferable IMO.

    So much about proper coding is in clarity/readability. One of my mantras is to always code for the poor engineer who has to read your code at 3am while hopped up on caffeine....

    In the first case, my own pattern recognition will look several times at the individual assignments and wonder what the reason may be for not using the 2nd well understood idiom. I'd also be forced to verify (painfully if the array were even a little larger) that the indexes were all increasing by 1.

  6. #6
    Junior Member
    Join Date
    Dec 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array definitions

    The first declaration allocates a variable "a" on the stack which points to the first element of the array on the heap. The heap has 4 spaces allocated for it. You then fill those spaces up with following declarations. This is called dynamic allocation. It's useful when you don't know exactly what's going to be in those allocated memory spaces. The second declaration allocates those variables directly on the stack.

Similar Threads

  1. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  2. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  3. keyword definitions needed
    By BLUEJ in forum Java Theory & Questions
    Replies: 5
    Last Post: December 8th, 2012, 04:56 PM
  4. Replies: 4
    Last Post: December 6th, 2011, 02:24 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM

Tags for this Thread