[Java beginner] Difference between length and length () ~ I don't know ~

Length and length () used to get the length in Java. I don't know the difference, so I summarized it.

It seems that the meaning is often confused even for non-beginners, so let's review it once.

◆ Difference between length and length ()

length: Get the length of the array </ b> length (): Get the length of the string </ b>

For reference, use it like this.

public class Main {
  public static void main(String[] args) {

    //length: Get the length of the array
    int[] array = {1, 2, 3};
    System.out.println(array.length);   // 3

    // length(): Get the length of the string
    String str = "length of length";
    System.out.println(str.length());   // 9
  }
}

By the way, when you get the length of a character string using length (), it is counted as one character regardless of whether it is full-width or half-width.

Let's implement the code after understanding which one you want to use. See you again

Recommended Posts