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?...
String class is immutable in java, so calling toLowerCase()
will return another string with the result of that operation
..the charAt() method should only work with a string reference variables?...
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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.