Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, 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 the actual address hence it is also called an 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:- 

 it will store data under heap memory with a separate memory block. It provides safe and individual memory.





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

if(s==s1)      //  == is used to compare value type not reference type
sout("equall")
else
sout("not equall");

o/p not equally

if(s.equals(s1))     // equals() is used to compare the value according to address
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.


Syntax of StringBuffer:-

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

Example of StringBuffer:-

public class Stringexample {

public static void main(String[] args) {
StringBuffer s = new StringBuffer("hello");
s.append("world");
System.out.println(s);
}

}


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.

Syntax of StringBuilder:-


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 methods of String?


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


Write a program to find the largest palindrome in a paragraph?

WAP to count total world in a paragraph?

WAP to count total upper case, lower case, special char, and white space in a paragraph?


WAP to convert String in the opposite case?












9 Comments

POST Answer of Questions and ASK to Doubt

  1. Write a program to define 10 predefine method of String?

    public class TenMethod {

    public static void main(String[] args) {
    String s3="java is fun to learn";
    String s;
    char arr[]= {'g','u','p','t','a'};

    String s2=new String(arr);
    String s1=new String("prashant");
    String s4="java";
    s= "welcome in java";
    System.out.println(s.toLowerCase());
    System.out.println(s.toUpperCase());
    System.out.println(s.concat(" for beginners"));
    System.out.println(s.substring(10));
    System.out.println(s.substring(4,10));
    System.out.println(s1.hashCode());
    System.out.println(s2);
    System.out.println(s3.charAt(2));
    System.out.println(s3.replace("fun", "easy"));
    System.out.println(s3.contains(s1));
    System.out.println(s3.contains(s4));

    char [] Array = s4.toCharArray();
    System.out.println("size of array" +Array.length);
    System.out.println(" last element of array is " + Array[3]);
    }

    }

    ReplyDelete
  2. class Pcount
    {
    public static void main(String arg[])
    {
    String s="Kangaroo soft private Limited";
    int x=s.length();
    int c=0;
    for(int i=0;i<x;i++)
    {
    if(s.charAt(i)==' ')
    {

    }
    else
    {
    c++;
    }
    }
    System.out.println(c);
    }
    }

    ReplyDelete
  3. class Method
    {
    public static void main(String arg[])
    {
    String s1="Hello Ankit";
    String s2="Goswami";
    System.out.println(s1.toUpperCase());
    System.out.println(s1.toLowerCase());
    System.out.println(s1.length());
    System.out.println(s1.equals(s2));
    System.out.println(s1.concat(s2));
    System.out.println(s1.lastIndexOf("t"));
    }
    }

    ReplyDelete
  4. class Convert
    {
    public static void main(String arg[])
    {
    String s="hello";
    char ch[]=s.toCharArray();
    for(int i=0;i<ch.length;i++)
    {
    ch[i]=(char)((int)ch[i]-32);
    }
    for(int i=0;i<ch.length;i++)
    {
    System.out.print(ch[i]);
    }
    }
    }

    ReplyDelete
    Replies
    1. It only convert lower case to upper case, try for all cases

      Delete
  5. class Count1
    {
    public static void main(String arg[])
    {
    String s=" ANkit Goswami";
    int x=s.length();
    int uppercase=0;
    int lowecase=0;
    for(int i=0;i='A'&& ch<='Z')
    {
    uppercase++;
    }
    if(ch>='a'&& ch<='z')
    {
    lowecase++;
    }
    }
    System.out.println(uppercase);
    System.out.println(lowecase);
    }
    }








    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post