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 4 of 4

Thread: variables and efficiency

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default variables and efficiency

    If I'm going to need to use (say) the size of an array several times then is it more efficient to declare a new variable and put the size of the array into it or is it more efficent to use .size to look it up again each time? (Sorry if this is a very niave question - I'm very new to this). Thanks in advance,
    Catkinq


  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: variables and efficiency

    I believe all of Java's Collections API classes store the size of the collection inside of a variable, so for all practical cases the efficiency of both methods are pretty much the same.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: variables and efficiency

    Well if you're actually using an array such as String[] then you can use the length variable whenever you wish, its a normal variable after all and not a method call.

    And if you decide to use an ArrayList and call the size() method all that does is return the instance member int size. So like helloworld says, no biggie really. Use whatever you wish without storing it in a new variable, in fact, storing it in a new variable might take more time

    // Json

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: variables and efficiency

    thank you for the replies

Similar Threads

  1. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  2. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM
  3. how to extract variables,keywords,operator...
    By Nanda in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2009, 10:19 PM
  4. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM
  5. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM