Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

String,StringBuffer and StringBuidler in Java

String:-

The string is the predefined class of JAVA which can contain a collection of char. String object value can not be changed dynamically from actual address hence it is also called Immutable Object.

We can Write String using two different ways:-

1) String as a reference

   String s = "hello";     //reference but store data in String pool, String pool memory will be created by JVM using extern()

String s1= "hello";

String s2= "hello";

only one String pool memory will be created.

if(s1==s2)
{
    sout("equall");
}
2)  String as an Object

    String s = new String("hello");
    String s1 = new String("hello");
    String s2 = new String("hello");

if(s==s1)
sout("equall")
else
sout("not equall");

o/p not equally

if(s.equals(s1))
sout("equall")
else
sout("not equall");

o/p equal because equals() will compare values using the corresponding address

It will store data in heap memory under JVM


The important question of String:-

class Stringexample
{
   public static void main(String args[])
   {
      String s = "hello";
      String s2=s.concat("world");
      System.out.println(s);
      System.out.println(s2);

   }


}


class Stringexample2
{
   public static void main(String args[])
   {
      String s = new String("hello");
      String s2=s;
      String s1="hello";
     
     // if(s.equals(s1))
      if(s1==s2)
      System.out.println("equall");
      else
      System.out.println("not equall");

   }


}


StringBuffer:-

 It provides mutable and synchronized objects in java means we can change String Buffer data from actual address and it can be used in the Multithreading process.

StringBuffer ref = new StringBuffer("data1");
ref.append("data");


StringBuidler in Java:-

It provides mutable  object in java means we can change String Builder data from actual address and it can not be used in Multithreading process because StringBuilder can not be Synchronized.

StringBuilder ref = new StringBuilder("data1");
ref.append("data"); System.out.print(ref) ;//data1data

 Example of StringBuffer Comparision:-

class Stringexample2
{
   public static void main(String args[])
   {
      String s = new String("hello");
      StringBuffer s1 = new StringBuffer("hello");
      StringBuffer s2 = new StringBuffer("hello");
      //if(s2.equals(s1))
      if(s2.toString().equals(s1.toString()))
      System.out.println("equall");
      else
      System.out.println("not equall");

   }


}


most imp question of String and StringBuffer?


class Stringexample2
{
   public static void main(String args[])
   {
      String s = new String("hello");
      StringBuffer s1 = new StringBuffer("hello");
      s1.append(s); //hello hello
      String s2=s.concat(s1.toString());  //hello hello hello
      s1.append(s2); 
      System.out.println(s1); 
   
      System.out.println(s);
      System.out.println(s2);

   }


}

..............................................................................................................

Assignment:-

Write a program to define 10 predefine method of String?


Write a program to define 5 different methods of String Buffer and String builder?







Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post