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

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...