Page 79 - CTS - CSA TP - Volume 2
P. 79

COMPUTER SOFTWARE APPLICATION - CITS





              •  String concatenation is a common operation in Java when dealing with text processing, output formatting,
                 and building dynamic strings for display or storage.
              Output:


















           TASK 2: String Length

              public class StringExample2 {
                  public static void main(String[] args) {
                      String str = “Java Programming”;
                      System.out.println(“String: “ + str);
                      // Get the length of the string
                      int length = str.length();

                    System.out.println(“Length of the String: “ + length);
                  }
              }


           Explanation:
              •  The length() method is a built-in method provided by the String class in Java.
              •  It returns an integer representing the number of characters in the string.
              •  The length includes all characters in the string, including whitespace characters such as spaces.
              •  In the example, the string “Java Programming” consists of 16 characters, including the space between
                 “Java” and “Programming”.
              •  The length() method is commonly used in string manipulation tasks, input validation, and text processing
                 to determine the size of the string dynamically at runtime.
              •  It’s important to note that the length of a string does not include the null character (\0) at the end of the
                 string, as Java strings are null-terminated.


           Output:



















                                                           64

                               CITS : IT & ITES - Computer Software Application - Exercise 92
   74   75   76   77   78   79   80   81   82   83   84