String, StringBuffer and StringBuidler in Java

16

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 objects in java means we can change String Builder data from the actual address and it can not be used in the 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 the total wold 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?
Tags

Post a Comment

16Comments

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
  6. #Program to validate Email ID
    class Email
    {
    public static void main(String args[])
    {
    String email = "xyz@gmail.com";
    int dpos=0,atpos=0,i;
    for(i=0;iatpos && (dpos-atpos)!=0 && (i-1)!=dpos)
    System.out.println("Valid Emailid");
    else
    System.out.println("Invalid Emailid");

    }


    }

    ReplyDelete
  7. //yogesh seroke
    //Reverse String

    class A
    {
    public static void main(String args[])
    {
    String s="abcdef";
    s.reverse();
    System.out.println(s);
    }

    ReplyDelete
  8. class A
    {
    public static void main(String args[])
    {
    StringBuffer s= new StringBuffer("abcdef");
    System.out.println(s);
    s.reverse();
    System.out.println(s);
    }
    }

    ReplyDelete
  9. //yogesh seroke
    //Strings methods

    class Stringmethods
    {
    public static void main(String args[])
    {
    String s="java";
    String s1="IS";
    String s2="programming";
    String s3="language";

    System.out.println(s.toUpperCase()+" upper case");

    System.out.println(s1.toLowerCase()+" lower case");

    System.out.println(s2.concat(" "+s3));

    System.out.println(s.substring(1));

    System.out.println(s.charAt(0));

    System.out.println(s.compareTo(s1) );

    System.out.println(s.codePointAt(2));

    System.out.println(s2.codePointBefore(3));

    System.out.println(s2.codePointCount(2,2));

    System.out.println(s2.length());
    }
    }

    ReplyDelete
  10. //yogesh seroke
    //String reverse case

    class StringReverse
    {
    public static void main(String args[])
    {
    StringBuffer s=new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    System.out.println(s);
    s.reverse();
    System.out.println(s);
    }
    }

    ReplyDelete
  11. package stringexample;

    public class Paragraph {

    public static void main(String[] args) {
    String s1 = "hello world naman aaaaaaa cdfdsfdsfdsfds ssssssssssssssssssssss dkudsg hfdshgf dhjsgf hjds gfhj gfhffhhjfgfhfgjgg ";
    String s = s1 + " ";
    int m=0;
    String word="",word1="";

    for(int i=0;i=0;j--)
    {
    rev = rev + word.charAt(j);
    }

    if(rev.equals(word))
    {
    if(m<word.length())
    {
    m = word.length();
    word1=word;
    }
    System.out.println(word);
    }

    word="";
    }
    }

    System.out.println("Max is "+word1);
    }

    }

    ReplyDelete
  12. //concat two String

    class Shubham
    {
    public static void main (String args[])
    {
    String s ="Kshetriya";
    String s1 ="Shubham";

    System.out.println(s1+' '+s);
    }
    }

    ReplyDelete
Post a Comment