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

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:

主站蜘蛛池模板: 铜梁县| 清水河县| 罗定市| 治县。| 广水市| 武安市| 桃江县| 太白县| 容城县| 怀化市| 新和县| 随州市| 台山市| 浙江省| 沙田区| 涡阳县| 治县。| 凌源市| 民权县| 安乡县| 德兴市| 永嘉县| 嘉定区| 乳山市| 来安县| 重庆市| 泸西县| 钟山县| 晋中市| 竹北市| 武威市| 民权县| 息烽县| 重庆市| 平潭县| 塔城市| 庆安县| 安龙县| 宁河县| 炉霍县| 五大连池市|