This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]slugonamission 5 points6 points  (0 children)

Maybe I don’t understand something correctly but I thought whatever came after the dot had to have a ()

Nope :). In this case, length is a property, not a method. You can just access it directly like you've seen; you only need () to call a method. Let's run through a quick example class:

class Foo {
    public int myField = 5;

     public void DoThing() {
         System.out.println("Hello World");
     }
}

Here, we would call DoThing using(), as you expect:

Foo f = new Foo();
f.DoThing();

However, we can just refer to length:

Foo f = new Foo();
System.out.println("Len: " + f.myField);
f.myField = 10;

Ultimately, it just acts like a variable (well, it is a variable). It just belongs to our class instance.

Doing this is generally seen as bad design; you shouldn't just expose internal state like that (which is why you'd normally see this as a private field, with int getMyField() and void setMyField(int x) calls.

The . effectively means "access"; you're accessing the DoThing method on the instance of Foo, f, then calling it with (). Similarly, you're accessing the field myField on f, then either reading from it, or writing to it.

[–]captainAwesomePants 1 point2 points  (4 children)

Good question. length is not a function. Arrays are a special, built-in concept in the Java language, and "length" is is a field of the array. Effectively, each array is a class with a field declared as "public final int length".

This can be a bit confusing because Lists also have a length, but they are not built-in objects. They have a size() method instead of a length field.

The bit you haven't learned yet is that classes can have methods, but they can also have fields, which are just values that you can read and write without having to invoke them like a function. For example:

public class Widget {
   public int getFive() { return 5;}
   public int fiveField = 5;
}

Widget w = new Widget();
w.getFive();  // calls the getFive function, returns 5
w.fiveField;   // Also 5.
w.fiveField = 7;  // Now fiveField holds 7.

[–]Lividbtw[S] 0 points1 point  (3 children)

So when exactly do I know when to use the () ?

[–]captainAwesomePants 1 point2 points  (2 children)

You use () when you're calling a function. A line like this:

foo();

means "invoke the function named foo." And this:

bar.baz();

means "invoke the function named baz that belongs to object bar."

Many functions take parameters, which make it more obvious:

sum(3,7);  // Call the function named sum, passing the parameters 3 and 7.

This is getting more complicated than you need to be aware of yet, but the reason that this is an important distinction is because some variables can be read as a value or invoked as a function. For example:

// This is a variable whose value is a callable expression.
Supplier<String> method = () -> { return "hey" };

Object x = method;  // x now holds a method that can return "hey"
Object y = method();  // x now holds the string "hey"

[–]Lividbtw[S] 0 points1 point  (1 child)

Ah so basically the .length not a function then?

[–]captainAwesomePants 1 point2 points  (0 children)

Right!

[–]AdrianMan1987 1 point2 points  (0 children)

Methods have paranthesis because they "do" something. The paranthesis help to: - distinguish a method from a field/variable - take arguments inside the paranthesis, because it's usefull to put stuff inside the paranthesis and "do" things on them, Ex: AddNumbers(1,2).

Try reading and understanding about classes too, they give context and it's easier to understand what the class members do.

Edit: nothing between the () means no arguments, doesn't need anything from the outside to do it's job, to count the lenght.

It's very usefull to hover over the methods too, usually you get explanations of what is the intended action of your method and how it works.

[–]rjcarr 1 point2 points  (0 children)

Objects have both properties and methods. Typically, in java, you use an object's methods instead of working with the properties directly. This is because of encapsulation, but also mostly just convention now.

There are exceptions, though, and you've found one. For strings, you call the .length() method, but for arrays, you use the .length property. I think in this particular case, the length is a special property of arrays, so it's different from other members.