When I use the method .length() I sometimes must use parenthesis and sometimes not. What is the rule for using this? For example:
int[] secArray = {1, 2, 3};
System.out.println(secArray[2]);
System.out.println(secArray.length);
While here, I must use it: (capLetter is a string)
System.out.println("Your name consists of " + capLetter.length() + " letters.");
I could not find any question nor answer about this. I am a beginner in Java, sorry if the question is ridiculous. Thanks in advance!
It depends on whether the object is implementing length as a field or a method.
Array.length
is a field.
String.length()
is a method
List.size()
is a method
In java, you will see methods more often than fields. Arrays are a special case since they are part of core java.