Tuesday 27 November 2012

STRING AND STRINGBUFFER CLASSES

A sequence is a list of individuality. String sessions are used to symbolizes these list of personality.
We can even make an item an item of a sequence category without using new owner instead of the Stringclass with the materials provides in "" and for each and every people personality within "" it designates a exclusive catalog value beginning from zero
       EX: String s= "abcde";
Address of the item is allocated to the varying s
     This item has near regards with  arrays. once a sequence item is designed the details within the item cannot be customized. This is the purpose why we say that post are immutable

DIFFERENT WAYS OF CREATING STRING OBJECTS ARE SHOWN BELOW.
         String S1=new String (); 
         String S2=new String ("abc")
         String S3="MSDASS"


Consider the following two statements
         (i) A a1 = new A (),(ii) String S1="abcde";
now when we say System.out.println (a1); it create the deal with of the item where as for System.out.println (s1); it printing abcde (i.e the material of the object) why?
For the first declaration internal item category toString () is implemented and which makes the deal with of the object
But for the second declaration internal sequence category toString () is implemented and it can created the meaningfull
representation
NOTE:-
  In String,Stringbuffer,Thread, Exemption, wrapper sessions and all the selection sessions already  the toString() is overridden in a meaningfull representation

QUESTION:
What is the distinction between allowing the pattern item using new owner and allowing the sequence item using dual quotes?
       Using new owner we can make many objects of the same sequence class
For example
                String s1=new String("xyz");
                String S2="xyz";


    Here though the material are same two different item are make and the details are allocated to different varying.

      But when we make objects of String category using dual quotations we can highest make only one item into the whole program of we try to make more objects the same deal with of already  designed objects.
EX:
        String S1="MSO";
        String S2="MSD";
  The above describe distinction is caved the following program
         category Sdemo1
           {
                     community fixed gap primary (String args[])
                        {
                               String s1="abc";
                               String S2= new String ("xyz");
                               String s3 = new String ("xyz");
                               if (s2==s3)
                                         system.out.println("s2 & s3 are equal");
                               else
                                          System.out.println("s2 &s3 are not equal"); // this  //statement is executed
                               String s4="abc";
                                if (s1==s4)
                                           System.out.println("s1 & s4 are equal");  //  this declaration is // executed
                                else
                                           system.out.println("s1 & s4 are not equal");
                            }
             }

The following program is another which indicales that using dual quotations we can make only one String category comprising a list of figures not only in the functionality but in the whole program
           category Sdemo2
               {
                   String s1="abc";
                   gap operate ()
                     {
                          String s1="abc";
                           if (s1 == this . s1)
                           System.out.println("s1 & this. s1 are equal");
                           else
                           System.out.println("s1 & this. s1 are not equal");
                            System.out.println("end of function");
                      }
                     Public fixed gap primary (string args [])
                      {
                            Sdemo2 d=new Sdemo2 ();
                            d.function ();
                            String s3="abc";
                             if (s3 == d.s1)
                             System.out.println("s3 & d.s1 are equal");
                             else
                             System.out.println("s1 & d.s1 are not equal");
                     }
            }

No comments:

Post a Comment