169

I come from a php background and in php, there is an array_size() function which tells you how many elements in the array are used.

Is there a similar method for a String[] array? Thanks.

3
  • 3
    Heh, all answers were within the same minute.
    – jjnguy
    Commented May 28, 2009 at 15:20
  • 1
    Ah, PHP with its cluttered global namespace. The good old days...
    – Kevin Ji
    Commented Dec 22, 2011 at 1:11
  • 2
    i think he means php's count() not array_size() Commented Feb 17, 2012 at 20:33

12 Answers 12

293

Yes, .length (property-like, not a method):

String[] array = new String[10];
int size = array.length;
2
  • 2
    hi, how could we find the docs that list all the possible properties and functions of array object? I don't mean the Arrays object which is the only docs that I can find.
    – Sam YC
    Commented Mar 28, 2013 at 4:57
  • 1
    @GMsoF The documentation for "[]" (array, not Arrays object) is actually the Java specification. .e.g docs.oracle.com/javase/specs/jls/se7/html/jls-10.html Edit: Ah... the comment is from 2013...
    – Acapulco
    Commented Jun 15, 2015 at 22:42
47
array.length

It is actually a final member of the array, not a method.

4
  • 2
    IIRC, it's not actually a field, it just looks like one. Commented May 28, 2009 at 15:21
  • Oh, then what is it? Some special Array thing?
    – jjnguy
    Commented May 28, 2009 at 15:28
  • 3
    a "pseudo field" which is compiled to a special array length op-code, rather than the field access op-code. Commented May 28, 2009 at 15:34
  • 2
    It is a field, accroding to §10.7 (java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7 ): "The public final field length, which contains the number of components of the array (length may be positive or zero)."
    – Michael Myers
    Commented May 28, 2009 at 15:34
19

Also, it's probably useful to note that if you have a multiple dimensional Array, you can get the respective dimension just by appending a '[0]' to the array you are querying until you arrive at the appropriate axis/tuple/dimension.

This is probably better explained with the following code:

public class Test {
    public static void main(String[] args){
        String[][] moo = new String[5][12];

        System.out.println(moo.length); //Prints the size of the First Dimension in the array
        System.out.println(moo[0].length);//Prints the size of the Second Dimension in the array
    }

}

Which produces the output:

5
12
15

array.length final property

it is public and final property. It is final because arrays in Java are immutable by size (but mutable by element's value)

14

Arrays are objects and they have a length field.

String[] haha = {"olle", "bulle"};

haha.length would be 2

7
  • Just to nitpick. But I believe arrays are NOT objects in Java - they are one of the two reference type of variables. The other being objects.
    – jabbie
    Commented May 28, 2009 at 15:43
  • 2
    @jabbie: Really? Do you have a reference for that? I only question it because you can say Object o = new String[] { "hello" }; Commented May 28, 2009 at 15:51
  • simon i think the reason that statement works is because they introduced the reflexive classes for all of the primitive types. Commented May 28, 2009 at 15:57
  • 9
    Read the first sentence of docs.oracle.com/javase/specs/jls/se8/html/jls-10.html and then try telling us that arrays aren't objects. ;)
    – Michael Myers
    Commented May 28, 2009 at 16:05
  • 1
    you could have used some other variable name, it's so distracting to me Commented Mar 12, 2013 at 10:32
11

In java there is a length field that you can use on any array to find out it's size:

    String[] s = new String[10];
    System.out.println(s.length);
1
  • 1
    thank you for not getting into technicalities about what it might or might not be, and simply providing a short and concise answer. +1 for you. :) Commented Feb 17, 2012 at 20:32
9

If you want a function to do this

Object array = new String[10];
int size = Array.getlength(array);

This can be useful if you don't know what type of array you have e.g. int[], byte[] or Object[].

9

There is a difference between length of String and array to clarify:

int a[] = {1, 2, 3, 4};
String s = "1234";

a.length //gives the length of the array

s.length() //gives the length of the string
1
  • This should be updated in the question if it is clarification.
    – Whitecat
    Commented Jun 15, 2015 at 22:52
5

The answer is "All of them". A java array is allocated with a fixed number of element slots. The "length" attribute will tell you how many. That number is immutable for the life of the array. For a resizable equivalent, you need one of the java.util.List classes - where you can use the size() method to find out how many elements are in use.

However, there's "In use" and then there's In Use. In an class object array, you can have element slots whose elements are null objects, so even though they count in the length attribute, but most people's definitions, they're not in use (YMMV, depending on the application). There's no builtin function for returning the null/non-null counts.

List objects have yet another definition of "In Use". To avoid excessive creation/destruction of the underlying storage structures, there's typically some padding in these classes. It's used internally, but isn't counted in the returned size() method. And if you attempt to access those items without expanding the List (via the add methods), you'll get an illegal index exception.

So for Lists, you can have "In Use" for non-null, committed elements, All committed elements (including null elements), or All elements, including the expansion space presently allocated.

2

All the above answers are proper. The important thing to observe is arrays have length attribute but not length method. Whenever you use strings and arrays in java the three basic models you might face are:

  1. String s=new String("vidyasagar");
    System.out.println(s.length()); // In this case we are using only String. No length attribute for Strings. we have to use length() method.
  2. int[] s=new int[10]; System.out.println(s.length); //here we use length attribute of arrays.
  3. String[] s=new String[10];
    System.out.println(s.length); // Here even though data type is String, it's not a single String. s is a reference for array of Strings. So we use length attribute of arrays to express how many strings can fit in that array.
1

Not really the answer to your question, but if you want to have something like an array that can grow and shrink you should not use an array in java. You are probably best of by using ArrayList or another List implementation.

You can then call size() on it to get it's size.

0

yes it's actually the str.length without () for example :

String[] strArray = {"how", "are", "you"};
strArray.length; //returns the length of the string

Not the answer you're looking for? Browse other questions tagged or ask your own question.