简体   繁体   English

用Java以这种方式“(firstName.toLowerCase())。charAt(0)”吗? 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?... toLowerCase()和charAt()方法一起?

String class is immutable in java, so calling toLowerCase() will return another string with the result of that operation 字符串类在Java中是不可变的,因此调用toLowerCase()将返回具有该操作结果的另一个字符串

..the charAt() method should only work with a string reference variables?... .. charAt()方法仅应使用字符串引用变量?

you can use a literal string as well 您也可以使用文字字符串

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

but as I said before, the method tolowerCase can be invoked on string objects and will return another string 但正如我之前所说,tolowerCase方法可以在字符串对象上调用,并将返回另一个字符串

you can (even that is not making much sense) call a sequence of method together 您可以(即使没有太大意义)一起调用方法序列

like: 喜欢:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM