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

COMPUTER SOFTWARE APPLICATION - CITS




           You’re absolutely correct! In Java, when you create a string using a string literal, the JVM checks the String
           Constant Pool. If an identical string exists in the pool, it returns a reference to that existing string instead of
           creating a new object. This behaviour is part of string interning, which helps conserve memory by reusing identical
           string instances.
           Immutable String in Java

           In Java, string objects are immutable, which means they cannot be modified or changed after creation. Instead,
           any operations that appear to modify a string actually create a new string object with the desired changes.
           class Demo {
               public static void main(String[] args)

               {
                   String name = “Sachin”;
                   name.concat(“ Tendulkar”);
                   System.out.println(name);

               }
           }
           Output :-


           Sachin

           class Demo {
               public static void main(String[] args)
               {
                   String name = “Sachin”;

                    name = name.concat(“ Tendulkar”);
                   System.out.println(name);
               }
           }


           Output :-

           Sachin Tendulkar




























                                                           312

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