What is difference between length and length() in java

This is also a confusing topic of java and most beginners get confused about length and length(). I was also confused about this topic. After reading this article you will never get confused.

So, We know that java is an Object Oriented Programming language we denote class as a template of objects and method is an action that an object is able to perform. You will see in java docs if we access any member of some class we use a method rather than a variable. Likewise,

  •  length():- length() is a function of string class that is used to get the length of a string. And
  • length:- length is variable for getting the length of an array.
Look At the example:-

Length() with string
public class FindLength {
    public static void main(String[] args) {
        String str = "this is skx coding";
        int strLength = str.length();
        System.out.println("length of str: "+ strLength);
    }
}
        

Output



Length with array
public class FindLength {
    public static void main(String[] args) {
        String[] str = new String[]{"SKX","Coding"};
        int strLength = str.length;
        System.out.println("length of str: "+ strLength);
    }
}
        

Output


If you have still Doubt Please comment below

Comments