官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 张家界市| 武山县| 灵川县| 泰安市| 五台县| 商南县| 达拉特旗| 五家渠市| 贺州市| 平遥县| 鄂尔多斯市| 新闻| 怀来县| 韩城市| 白朗县| 北票市| 泰顺县| 浪卡子县| 甘南县| 广饶县| 宁阳县| 宁都县| 永吉县| 定陶县| 韶山市| 扎囊县| 曲靖市| 榆林市| 阿城市| 宁海县| 山丹县| 新野县| 涞源县| 崇明县| 桓仁| 和林格尔县| 沁水县| 永川市| 邢台县| 大渡口区| 黔西|