Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Length, size or capacity in Java?

You get the length of an array, but you ask about the size() of an ArrayList, and you can inquire into the capacity() of a StringBuffer. Aren't they really all the same sort of thing? Which does Java use in which circumstance?

Java uses the word length when you're asking about something that can be determined by telling you the distance from the start to the end of something - thus an array, with fixed size elements arranged sequentially in memory, is going to be a length.

Java uses size where you're looking at a measure of the number of elements in something which is not in successive memory locations, or may not be, or where the elements themselves differ in size so you can't find out how many of them there are by measuring the distance from start to end.

And Java uses capacity where you're being told how much something can hold, and not how much it's holding at the moment.

Do you add brackets after the length / capacity / size word? If you're looking at a variable within the object, you don't, but if you're calling a method you do. Which is it in which circumstances? I have no magic answer - you just need to learn / know.




myarray.length - array length - [example]

MyArrayList.size() - number of elements in an Array List - [example]

MyString.length() - String length - [example]

MyStringBuffer.length() - String Buffer length - [example]

MyFile.length() - Number of bytes in a file - [example]

MyVector.size() - Number of elements in a Vector [example] and [example in Servlet]

MyAppletDimension.size() - returns an object with the width and height of an applet. This is deprecated and you should now use getsize ...

MyAppletDimension.getsize() - returns an object with the width and height of an applet - [example]

MyStringBuffer.capacity() - the number of characters that a StringBuffer can currently hold

(written 2010-02-24, updated 2010-03-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J707 - Java - Strings
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [1557] Trying out our Java examples on our web site - (2008-02-27)
  [1446] An answer to a student asking 'Help' - (2007-11-27)

J714 - Java - Fundamental classes
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2734] for and foreach in Java - (2010-04-22)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [1910] Java - Generics - (2008-11-27)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [42] Do languages change? - (2004-09-08)

J802 - Java - Abstract Windowing Toolkit
  [3049] Computer Graphics is fun - even if Java Applets are Old Hat - (2010-11-13)
  [2654] Java - from applets to servlets. JSPs and more - (2010-02-27)
  [2245] Client side (Applet) and Server side (Servlet) Graphics in Java - (2009-06-17)
  [1325] Java - Client side applet applications as well as server side - (2007-08-29)


Back to
Java arrays - are they true arrays or not?
Previous and next
or
Horse's mouth home
Forward to
Getting a phone line changed ...
Some other Articles
Java Server Pages - putting it all together
Reading and writing cookies in Java Servlets and JSPs
Calculation within objects - early, last minute, or cached?
Getting a phone line changed ...
Length, size or capacity in Java?
Java arrays - are they true arrays or not?
Removing duplicates from a MySQL table
Compile but do not run PHP - syntax check only
Optimising and caching your MySQL enquiries
Counting rows in joined MySQL tables
4318 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2649_Len ... Java-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat