Page 235 - computer science (868) class 11
P. 235

09


                                                                                   STRINGS
















                          Learning Objectives



                     9.1  Character Class                                9.2  String Class
                     9.3  StringBuffer Class                             9.4  String Methods under Wrapper Class
                     9.5  StringTokenizer Class                          9.6  Java Library Class





                 In programming, Strings refer to the combination of different characters including alphabets, digits, punctuators
                 and/or special symbols. However, these are to be placed between double quotes (“ ”) and we have to use the keyword
                 “String” to store it in a variable.

                 Let us understand this better with an example. The below statement would represent a String:
                    "India with an area of 3,287,263 km², is the 7th largest country in the world and
                    is about one-third the size of the USA."
                 In the above statement, we can see that there are digits also used along with alphabets, but since they are all enclosed
                 within the “ ”, they are referred to as String data type. Let us consider the following examples to understand this:
                    String a = "4567";
                    System.out.println(a);
                 This would give the output:

                 4567

                    String a = 4567;
                    System.out.println(a);
                 This would give an error saying incompatible type.

                 So, to work with Strings we not only need to use the keyword “String”, but also remember to enclose the value within
                 double quotes “ ”.
                 The size of the string variable depends on the length of the string. The length of the string depends on the number
                 of characters the string contains and each character holds 2 bytes of memory space. So, if a string is composed of 5
                 letters, then the size it will occupy in memory will be 5 × 2 = 10 bytes.
                 Let us next proceed to comprehend the Character class which is used to form the strings.





                                                                                                                       233
                                                                                                              Strings  233
   230   231   232   233   234   235   236   237   238   239   240