Page 347 - Computer science 868 Class 12
P. 347

10


                                                                                    STRINGS

















                        Learning Objectives


                    10.1  ASCII – American Standard Code for Information Interchange
                    10.2  String Functions                                  10.3   String Buffer Function
                    10.4  Conversion from String to Primitive Data Types and Vice Versa  10.5   Stringtokenizer Class




                 We know that the strings are the combination of different characters. All letters, digits, space and punctuators together
                 make a String. To declare a string, the following syntax is used.

                 String sent= “India has 28 states and 9 Union Territories.”  Anything inside double quotation marks “ ” is termed as a string.
                 As we can see a string is a combination of individual characters. So, the size of the string will be the number of characters
                 × 2. This is because each character occupies 2 bytes. The above sentence will occupy 44 × 2 = 88 bytes in memory.

                 Before starting to work with string, we must first have an idea of characters. Any letter, digit, or special character when
                 written in within single quotation marks is known as a Character in Java. A character can be declared using the data
                 type “char”.

                 For example, char ch= 'a';
                 In the above example, ‘ch’ is the character variable which holds the character ‘a’ and it takes 2 bytes space in the
                 memory.
                 Similarly, a character can also be taken from user as an input. To input a character, the following syntax is used.

                 For example, char ch=sc.next().charAt(0);
                 Java has provided many built-in methods to work with characters. The class under which these built-in methods resides
                 is known as the “Character” class.

                                           Method                                      Description

                         Character.isLetter(char)                     This method checks whether the given character in
                                                                      the argument is a letter or not. If letter, then it will
                                                                      return true else false.

                         Character.isDigit(char)                      This  method  checks whether the given character
                                                                      in the argument is a digit or not. If digit, then it will
                                                                      return true else false.



                                                                                                                       345
                                                                                                              Strings  345
   342   343   344   345   346   347   348   349   350   351   352