Core Java Articles


Total available count: 12
Subject - Java Technologies
Subsubject - Core Java

About String and StringBuffer classes with Examples

String class is the non mutable class whereas StringBuffer is the mutable class. String class contain overriding equals() method uses concept Content comparison return type is Boolean. StringBuffer class uses Object class equals method which return Boolean. String and StringBuffer present in the final classes present in the java.Lang package. StringBuffer class contains methods like insert(), append() and replace().

class Object
{
 public String toString()
	{return "[email protected]";
	}
}
class String extends Object
{   //overriding tostring()
	 public String toString()
	{return "contents of the String object";}
}
class StringBuffer extends Object
{    //overriding tostring()
	 public String toString()
	{return "contents of the StringBuffer object";}
}

 

Example:

class  Test
{  Test(String str){}
	public static void main(String[] args) 
	{  //object class -->ref-comp
	   Test t1 = new Test("Slightbook");
	   Test t2 = new Test("Slightbook");
	   System.out.println(t1.equals(t2));//false
	   //string class content comp
	   String s1="Web";
	   String s2="Web";
	   System.out.println(s1.equals(s2));//True
//StringBuffer is not overriding -->it uses object class equals() method.
       StringBuffer sb1 = new StringBuffer("Author");
	   StringBuffer sb2 = new StringBuffer("Author");
	   System.out.println(sb1.equals(sb2));//false
     }
}

 

Output:

false
true
False

class Object
{ public boolean equals()
{return "ref-comp";}//if two refernce variables points to same objects return true.
}
class String extends Object
{//overriding equals()
} public boolean equals(){ return "content comp";}
class StringBuffer extends Object
{  //not overriding equals() means it will implement Object class
}

 

Now see the example for conversion of string to stringbuffer and stringbuffer to string.

class Test 
{ public static void main(String[] args)
	{
		//String to StringBuffer because reversing of string is not present String class but StringBuffer class contain method 
		String str = "Author";
		StringBuffer sb = new StringBuffer(str);
		System.out.println(sb.reverse()); 
        //convert StringBuffer  to String
        StringBuffer sb1 = new StringBuffer("Slightbook");
		String ss = sb1.toString();
		System.out.println(ss);
    }
}

 

Difference between equals() and compareTo() methods is, in the equals() the return value is boolean but in the compareTo() return type is the either 0 or 1 or negative value(a=95,A=65). if return value is 0 then both are equal.

class Test 
{ public static void main(String[] args)
	{   //string class comparitive method() executed
		String s1="Slightbook";
		String s2="Author";
		String s3="Slightbook";
		System.out.println(s1.equals(s2));//false
		System.out.println(s1.equals(s3));//True
		System.out.println(s3.equals(s2));//false
		System.out.println("SLIGHTBOOK Web AUTHOR".equals("slightbook web author"));//false
		System.out.println("SLIGHTBOOK Web AUTHOR".equalsIgnoreCase("slightbook web author"));//true
		//compareTo() ---------->int
        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s3.compareTo(s2));
		System.out.println("SLIGHTBOOK Web AUTHOR".compareTo("slightbook web author"));
		System.out.println("SLIGHTBOOK Web AUTHOR".compareToIgnoreCase("slightbook web author"));
	}
}

 




Next 5 article(s)

1
About Static blocks
2
Concept of Inheritance
3
Types of Inheritance
4
Usage of Instance block, Static block and Super class variables in Inheritance
5
Usage of 'this' and 'super' keywords

Comments