Page 1 of 2 12 LastLast
Results 1 to 20 of 35
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default understanding the method "length" and how to use it

    Hello again,

    so in the following code, again from the Java oracle tutorial, there is a this little line below (in red) which is confusing me. I don't understand why it is length and not length()? Also, why is nothing imported? no package, nothing, so what package does it come from?

    Java Code:
    public class Adder {
        public static void main(String[] args) {
    	[COLOR="red"]int numArgs = args.length;
    [/COLOR]
    	//this program requires at least two arguments on the command line
            if (numArgs < 2) {
                System.out.println("This program requires two command-line arguments.");
            } else {
    	    int sum = 0;
    
    	    for (int i = 0; i < numArgs; i++) {
                    sum += Integer.valueOf(args[i]).intValue();
    	    }
    
    	    //print the sum
                System.out.println(sum);
            }
        }
    }
    Thank you!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    I don't understand why it is length and not length()
    Because it's a field, not a method.

    Names
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    length is a public attribute of an array.
    There is no length() method.

    As for no imports, that code is not using anything that isn't in the java.lang package, so no import statements are needed.

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by DarrylBurke View Post
    Quote Originally Posted by Tolls View Post
    length is a public attribute of an array.
    There is no length() method.

    As for no imports, that code is not using anything that isn't in the java.lang package, so no import statements are needed.
    How do you know that? Where in the language did you find that? I'm just looking for a class or package where I can read about it. I found the example DB, but I still don't know where it comes from. Also, speaking of arrays:

    The array class defined in the API:

    java.lang.Object
    extended by java.lang.reflect.Array

    has a This method:

    static int getLength(Object array)
    Returns the length of the specified array object, as an int.

    Why couldn't I use it o get the size of an array?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Because an array (ie int[]) is not an Array.
    It is a built in thing, which has a built in property called length.
    From the tutorial in Darryl's post:
    Finally, you can use the built-in length property to determine the size of any array.
    Now, you could use that static getLength method in Array if you wanted, but it would be a bit pointless.

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Oh okay. Thanks!

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    You could use the method call but its the long way around:

    int[] anArray = new int[5];
    System.out.println("length=" + anArray.length + " and " + java.lang.reflect.Array.getLength(anArray));
    >>> length=5 and 5

  8. #8
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    You could use the method call but its the long way around:

    int[] anArray = new int[5];
    System.out.println("length=" + anArray.length + " and " + java.lang.reflect.Array.getLength(anArray));
    >>> length=5 and 5
    This is precisely what I wanted to know. So I have to use the full name of the class to get it to work, but it's possible to use it. Thanks.

    So When do you use the full name of the class? Since it's in java.lang shouldn't it automatically be available?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Try it and see what happens. Then you'll know and can tell us what happens.

  10. #10
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Sweet. Alright, I tried both:

    So obviously, your way with the full named worked out.
    Without it I get an error "cannot find symbol"

    So I added an import statement:

    Java Code:
    import java.lang.reflect.*;
    and used

    Java Code:
    Array.getLength(myarray);
    and it worked. Now I have a new question. Tolls made the point of indicating that array and Array are not the same which I understand that Java is case-sensitive, so why did it work?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    so why did it work?
    What is the "it" that worked?

  12. #12
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Oh sorry.

    The size of the array was returned using either statements that is array.length; and java.lang.reflect.Array.getLength(array);

    so then when I imported the class, the shorter statement Array.getLength(array); also returned the size of the array.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    That's what import statements allow you to do when referring to class names.
    When you import the package, you can code the class name without the package name.

    There can be confusion when you use variable names the same as class names but with different case: array and Array.
    Better to use your own unique variable name: myArray

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Which is what that method does.
    Why is that odd?

  15. #15
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    That's what import statements allow you to do when referring to class names.
    When you import the package, you can code the class name without the package name.

    There can be confusion when you use variable names the same as class names but with different case: array and Array.
    Better to use your own unique variable name: myArray
    Okay, thanks.

    Quote Originally Posted by Tolls View Post
    Which is what that method does.
    Why is that odd?
    hello Tolls, this is (or was) odd to me because I was mistakenly assuming that the word array was a reserved word in Java and that it meant something. It was my fault, all part of learning.

    I now have another question though:

    http://download.oracle.com/javase/6/...ect/Array.html

    If you go there, there is these couple of lines which are now throwing me off:

    Quote Originally Posted by Java API Array Class
    newInstance

    public static Object newInstance(Class<?> componentType,
    int length)
    throws NegativeArraySizeException

    Creates a new array with the specified component type and length. Invoking this method is equivalent to creating an array as follows:

    int[] x = {length};
    Array.newInstance(componentType, x);

    I attempted to create an array using the code in red and I entered the number 16. When I tried to ask for the length, it returned the number 1. Am I missing something?
    Java Code:
    int[] x = {16};
    system.out.println(x.length); //prints 1
    
    int[] x;			    //declares array of integers named imgNum
    x = new int[16];              //allocates memory for 16 integers
    System.out.println(x.length);  //prints 16
    Why is that?

    Is there a single statement way of declaring an array AND to allocate the right memory size for it? If so, what is it please :)

    Thanks!

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    The newInstance method takes an array for a parameter. Read the doc for what the contents of that array is for. It looks like the contents here is the length of something. Perhaps the length of the first dimension of the array being created. Just a guess. I have not read the doc.
    Is there a single statement way of declaring an array AND to allocate the right memory size for it?
    int[] anArray = new int[25]; // create an array with 25 elements

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigsonny View Post
    Is there a single statement way of declaring an array AND to allocate the right memory size for it? If so, what is it please :)
    Sure, an initializer can do it:

    Java Code:
    int[] a= { 1, 2, 3 }; // creates an array with three elements, 1, 2 and 3
    int[]b= new int[3]; // also creates an array with three elements, all zero
    int[]c= { 3 }; // creates an array with one element, its value is 3
    Does this clarify things a bit?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    Sure, an initializer can do it:

    Java Code:
    int[] a= { 1, 2, 3 }; // creates an array with three elements, 1, 2 and 3
    int[]b= new int[3]; // also creates an array with three elements, all zero
    int[]c= { 3 }; // creates an array with one element, its value is 3
    Does this clarify things a bit?

    kind regards,

    Jos
    Yes, it does perfectly. Thanks!

  19. #19
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    The newInstance method takes an array for a parameter. Read the doc for what the contents of that array is for. It looks like the contents here is the length of something. Perhaps the length of the first dimension of the array being created. Just a guess. I have not read the doc.

    int[] anArray = new int[25]; // create an array with 25 elements
    I went back and re-read it and I don't get it. It uses {} and states this about the parameters

    Parameters:
    componentType - the Class object representing the component type of the new array
    length - the length of the new array
    So wouldn't that be [10] instead of {10}? Also, the Class object representing component type, I'm assuming that this means Integer, Character, String, etc...Is that a correct assumption?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. Replies: 2
    Last Post: 04-28-2009, 04:52 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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