“.length” 可以用来获取数组的长度。
int a = new int[10];
System.out.println(a.length)
“.length()” 用于获取String字符串中字符的个数。
String str = "nihaoAfriend";
System.out.println(str.length())
String中length()方法源码,底层也是将String转换成char数组,调用length属性:
/**
* Returns the length of this string.
* The length is equal to the number of <a href="Character.html#unicode">Unicode
* code units</a> in the string.
*
* @return the length of the sequence of characters represented by this
* object.
*/
public int length() {
return value.length >> coder();
}