0

This might sound very basic to some of you, but I am confused about the use of () in the below code in JAVA. The code works I just need to clear some basics

public class Main {

public static void main(String[] args) {

    String message = "  Hello" + " " + "!!  ";
    System.out.println(message.length());


    int[] numbers = {2,3,4,5,6};
    System.out.println(numbers.length);

   }
}

Why do I have to use parenthesis in the end for when getting the length differently?

thanks for the help

  • 1
    Not sure if the duplicate link covers the `String#length()` method, but a string is an object, and so has methods. – Tim Biegeleisen Oct 30 '20 at 10:03
  • 1
    Just because the two *things* are both called `length` does not mean anything. In one case it is a field, in the other it is a method, they have nothing to do with each other. – luk2302 Oct 30 '20 at 10:06
  • @TimBiegeleisen Arrays in Java are also objects, and have methods (just only those defined on `Object`). – Mark Rotteveel Oct 30 '20 at 10:18

0 Answers0