美团面试官问我一个字符的String.length()是多少,我说是1,面试官说你回去好好学一下吧
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 本文首发于微信公众号:程序员乔戈里 public class testT { public static void main(String [] args){ String A = "hi你是乔戈里"; System.out.println(A.length()); } } 以上结果输出为7。 小萌边说边在IDEA中的win环境下选中String.length()函数,使用 ctrl+B快捷键 进入到String.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; } 接着使用 google翻译 对这段英文进行了翻译,得到了大体意思:返回字符串的长度,这一长度等于字符串中的