“(firstName.toLowerCase()).charAt(0)”以这种方式与 Java? charAt() 方法应该只适用于字符串引用变量?

"(firstName.toLowerCase()).charAt(0)" in this manner with Java? the charAt() method should only work with a string reference variables?

String firstName, middleName, lastName; 
char firstInitial, middleInitial, lastInitial;

firstName = "Huckle";
middleName = "Berry";
lastName = "Fin";

firstInitial= (firstName.toLowerCase()).charAt(0);
middleInitial = middleName.charAt(0);
lastInitial = lastName.charAt(0);

System.out.print(firstInitial);
System.out.print(middleInitial);
System.out.println(lastInitial);

...why is it possible to chain these methods. toLowerCase() and charAt() methods together?...

字符串 class 在 java 中是不可变的,因此调用 toLowerCase() 将 return 另一个具有该操作结果的字符串

..the charAt() method should only work with a string reference variables?...

你也可以使用文字字符串

firstInitial = "Huckle".toLowerCase().charAt(0);
middleInitial = middleName.charAt(0);
lastInitial = "Fin".charAt(0);

但正如我之前所说,可以在字符串对象上调用 tolowerCase 方法并将 return 另一个字符串

你可以(即使这没有多大意义)一起调用一系列方法

喜欢:

firstInitial = firstName
        .toLowerCase()
        .toUpperCase()
        .substring(0)
        .toLowerCase()
        .toUpperCase()
        .trim()
        .charAt(0);