Page 323 - CITS - Computer Software Application -TT
P. 323

COMPUTER SOFTWARE APPLICATION - CITS




           Example
           public class BuilderTest{
               public static void main(String[] args){
                   StringBuilder builder=new StringBuilder(“hello”);
                   builder.append(“Pradyumn”);

                   System.out.println(builder);
               }
           }
           Output



           Indeed, the Java String class offers a wide range of methods for performing various operations on strings.
           Some of the commonly used String methods include:
           •  compare(): Compares two strings lexicographically.

           •  concat(): Concatenates one string with another.
           •  equals(): Compares two strings for equality.
           •  split(): Splits a string into an array of substrings based on a delimiter.
           •  length(): Returns the length (number of characters) of a string.
           •  replace(): Replaces occurrences of a specified substring with another substring.

           •  compareTo(): Compares two strings lexicographically and returns an integer.
           •  intern(): Returns a canonical representation of the string.
           •  substring(): Extracts a portion of the string based on given indices etc.
           There are two ways to create String object
           1  By string literal

           2  By new keyword
           <String_Type> <string_variable> = “<sequence_of_string>”;
           1  String literal
           One of the advantages of using the `intern()` method in Java is to enhance memory efficiency. This is achieved
           by preventing the creation of new objects if an identical string already exists in the string constant pool.
           Example:
           String demoString = “ctiworld”;
           =

           In the statement String s = new String(“Welcome”);, the JVM operates as follows:
           It creates a new string object in the regular (non-pool) heap memory, and the literal “Welcome” is placed in
           the string constant pool. Consequently, the variable s will point to the object in the heap (non-pool) memory.
           Example

           String demoString = new String (“ctiworld”);
           Interfaces and Classes in Strings in Java
           CharBuffer  is  a  class  that  implements  the  CharSequence  interface.  Its  primary  purpose  is  to  enable  the
           substitution of character buffers for CharSequences. An illustrative application of this capability can be found
           in the java.util.regex package, where CharBuffer is used.





                                                           310

                              CITS : IT&ITES - Computer Software Application - Lesson 78 - 84
   318   319   320   321   322   323   324   325   326   327   328