Page 417 - CA_Blue( J )_Class10
P. 417

16


                                                                     STRING HANDLING
















                           Learning Objectives



                     16.1 The String Class                          16.2 How String is Stored in Memory?
                     16.3 Methods of the String Class               16.4 String Array





                 Till now, you have learnt about different data types of Java. You have used char data type to store a single character at a
                 time. Sometime, you may need to store a set of characters or a word in a variable. For this, you need to create a character
                 array, which is a time-consuming and tedious task. To overcome this problem, Java provides a bulti-in class named String.


                     16.1 THE STRING CLASS
                 A string is a sequence of characters enclosed between double quotes. Java provides a String class to store a string.
                 The String class is available under the java.lang package of Java Class Library (JCL). It helps to store a set of characters
                 directly in the object of the String class. To use the String class, we need to declare a variable of String type and to
                 assign a string value to the variable. Ensure that the value should be enclosed in double quotes.
                 There are different types of values that can be assigned as a String. They may be set of alphanumeric letters such as
                 “Kolkata – 700115”, set of letters such as “India is my country” or only set of digits written within double quotations
                 such as “12345679”.
                 Since String in Java is a built-in class, so to use the String variables we need to declare an object. There are two ways to
                 declare a String variable in Java which are implicit String declaration and explicit String declaration.

                 16.1.1 Implicit String Declaration
                 The word implicit means automatically. It is the process of direct assignment. So, to declare a String using this technique,
                 we need to write the sequence of letter/digits within double quotes and using assignment operator simply store it in
                 a String variable. For example,

                    String str = "Java is a High-Level Language";
                    String wrd= "India";
                 When you declare a String variable using this technique, Java compiler will automatically create an object of the String class.

                 16.1.2 Explicit String Declaration
                 We can also declare a String by using the new keyword. The new keyword creates an object of the String class explicitly.
                 The constructor of the String class is also used which may or may not accept a String value as parameter. The syntax to
                 create object of the String class is:
                    String NameOfObject = new String(parameter);

                                                                                                                       415
                                                                                                       String Handling  415
   412   413   414   415   416   417   418   419   420   421   422