Page 259 - Cs_withBlue_J_C11_Flipbook
P. 259

10


                                                                                   STRINGS
















                          Learning Objectives



                     10.1 Character Class                                10.2  String Class
                     10.3 StringBuffer Class                             10.4  String Methods under Wrapper Class
                     10.5 StringTokenizer Class                          10.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.





                                                                                                                       257
                                                                                                              Strings  257
   254   255   256   257   258   259   260   261   262   263   264