Skip to main content

String, StringBuffer and StringBuidler in Java


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?

Comments

  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

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...