- Java Programming for Beginners
- Mark Lassoff
- 500字
- 2021-07-02 15:22:47
The toUpperCase() function
So I promised you that strings had the functionality not seen in a simple primitive type. To employ this, let's navigate to the String class in our Java documentation at docs.oracle.com/javase/7/docs/api/. Select java.lang shown under Packages, then scroll down and select String from ClassSummary. As with the documentation for all Java classes, the String documentation contains Method Summary, which will tell us about all the functions we can call on an existing String object. If we scroll down in Method Summary, we'll come to the toUpperCase() function that converts all the characters in a string into uppercase letters:

Let's employ this function now. Back in NetBeans, we now need to determine the best place in our program to employ our toUpperCase() function:
package stringsinjava; public class StringsInJava { public static void main(String[] args) { char c = 'c'; String s1 = "stringone"; String s2 = "stringtwo"; String s3 = s1 + s2 + "LIT"; System.out.println(s3); } }
We know we need to employ the toUpperCase() function on the s3 string after finalizing the value of s3 in our StringsInJava.java program. We could do either of these two things:
- Employ the function on the line immediately after finalizing the value of s3 (by simply typing s3.toUpperCase();).
- Call the function as part of the line where we would print out the value of s3. Instead of printing out the value of s3, we could simply print out the value of s3.toUpperCase(), as shown in the following code block:
package stringsinjava; public class StringsInJava { public static void main(String[] args) { char c = 'c'; String s1 = "stringone"; String s2 = "stringtwo"; String s3 = s1+s2+"LIT"; System.out.println(s3.toUpperCase()); } }
If you remember from our documentation, the toUpperCase() function requires no arguments. It knows that it's being called by s3, and that's all the knowledge it needs, but we still provide the double empty parentheses so that Java knows we are in fact making a function call. If we run this program now, we'll get the uppercased version of the string, as expected:

But, it's important we understand what's going on behind the scenes here. The System.out.println(s3.toUpperCase()); code line is not modifying the value of s3 and then printing out that value. Rather, our println statement evaluates s3.toUpperCase() and then prints out the string returned by that function. To see that the actual value of s3 is not modified by this function call, we can print the value of s3 again:
System.out.println(s3.toUpperCase()); System.out.println(s3);
We can see that s3 keeps its lowercase components:

If we want to permanently modify the value of s3, we could do that on the previous line, and we could set the value of s3 to the function's result:
package stringsinjava; public class StringsInJava { public static void main(String[] args) { char c = 'c'; String s1 = "stringone"; String s2 = "stringtwo"; String s3 = s1 + s2 + "LIT"; s3 = s3.toUpperCase(); System.out.println(s3); System.out.println(s3); } }
The following is the output of the preceding code:

- 一步一步學Spring Boot 2:微服務項目實戰
- 數據庫程序員面試筆試真題與解析
- 青少年軟件編程基礎與實戰(圖形化編程三級)
- HTML5+CSS3網站設計教程
- 征服RIA
- Learning OpenStack Networking(Neutron)
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Cybersecurity Attacks:Red Team Strategies
- Mastering React
- Learning VMware vSphere
- Python趣味編程與精彩實例
- Java EE 7 with GlassFish 4 Application Server
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- Scrapy網絡爬蟲實戰
- Three.js權威指南:在網頁上創建3D圖形和動畫的方法與實踐(原書第4版)