length vs length()




length : length is a final variable applicable for arrays.
length variable represents the size the array .

int[] x = new int[4];

System.out.println(x.length());  CE: cannot find symbol method length()
System.out.println(x.length);   4 


length() : length() method is final method applicable for string objects. length() method return number of characters present in the string.

Interview Question:

Que: string[] s = {"a","aa","aaa"};

s.length;    3
s.length(); CE: cannot find symbol method length() in class String[].
s[0].length(); 1



In Multi dimensional array length variable represents only base size not the total size.
ex: int[][] x = new int[6][3];
x.length; --> 6

There is no direct way to find total length of multi dimensional array but indirectly we can find as follows:
x[0].length + x[1].length + ........x[n].length;


Comments

Popular posts from this blog

Variables (Part-1)

Arrays in Java