StringBuffer and StringBuilder

StringBuffer and StringBuilder

package org.day.strings;

import java.io.BufferedReader;

public class Test { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer(); sb1.append("Daya");

    StringBuilder sbu2 = new StringBuilder();
    sbu2.append("Daya");

    System.out.println(sb1.equals(sbu2));
            /* why sb1 and sbu2 are not equal */
}

}

View Answers

July 22, 2011 at 1:24 AM

it is coz whenever objects are created with new operator, a new memory is allocated and hence new reference id generated;

so sb1 and sbu2 will get different refence ids;

here equals method of Object class will work it compares two refernce id which are not equal.

Ads









Related Tutorials/Questions & Answers:
StringBuffer and StringBuilder
StringBuffer and StringBuilder  package org.day.strings; import...[] args) { StringBuffer sb1 = new StringBuffer(); sb1.append("Daya"); StringBuilder sbu2 = new StringBuilder(); sbu2.append("Daya
StringBuffer
StringBuffer  What's the difference between these two formats? String string=args[0]; String reverse = new StringBuffer(string).reverse().toString(); String string=args[0]; StringBuffer rev = string.reverse(); String
Advertisements
StringBuffer related.
StringBuffer related.  how to make StringBuffer as immutable
ModuleNotFoundError: No module named 'StringBuilder'
ModuleNotFoundError: No module named 'StringBuilder'  Hi, My... 'StringBuilder' How to remove the ModuleNotFoundError: No module named 'StringBuilder' error? Thanks   Hi, In your python
Java - StringBuffer class in Java
Java - StringBuffer class in Java       In this example you will learn about StringBuffer class. This example explains how you can use functions provided by the StringBuffer
Write a method which will remove any given character from a string?
. In this tutorial we have used StringBuilder is a mutable sequence of character. It is used as a replacement of StringBuffer. StringBuilder is not synchronized and uses... StringBuilder is used: The majority of the modification methods on this class
String reverse in java
StringBuffer and StringBuilder methods. For that we will do it by recursive way as well...; java API provides StringBuffer and StringBuilder  reverse() method which... String Buffer "); String reverses = new StringBuffer(word1).reverse
Creation of StringBuffer
Creation of StringBuffer      ... we have used the following StringBuffer Constructors. StringBuffer() //Default Constructor with 16 characters set StringBuffer(String s) //String to String
String Buffer insert and append example
with StringBuffer and append data to it. The StringBuffer is a class that implements...: StringBuffer(int len): This is the constructor of StringBuffer class... of StringBuffer. It takes following parameters:     pos
Appending String in java
. StringBuilder are in replacement for StringBuffer where synchronization is applied  that is used by single thread. StringBuilder principal operation is insert.... Methods summary for StringBuilder append() method which take any data type
String Reverse In Java Example
can use the reverse() method of StringBuilder class (jdk 1.5 and later) and StringBuffer class (jdk earlier than 1.5). reverse() method changes the sequence... to display the String in reverse order. Syntax of StringBuilder reverse() method
How To Append String In Java
append() method of either StringBuffer(prior to JDK 5) class or StringBuilder...; StringBuilder sb = new StringBuilder(); sb.append(str1).append(str2).append(str3
How many objects are created in the code below? Explain.
"; StringBuffer s2 = new StringBuffer(s1); StringBuffer s3= s2; StringBuffer s4 = new StringBuffer(s1
explain the difference between string builder and string buffer classes
= "abc"; StringBuffer s2 = new StringBuffer(s1); StringBuffer s3 = s2; StringBuffer s4 = new StringBuffer(s1); } in this example how many objects are created
Appending String efficiently in Java
Appending String efficiently in Java  Hi, Share me the example code for Appending String efficiently in Java. Thanks   Hi, StringBuilder... va3 ="C"; Then you can do like this: StringBuilder sb = new StringBuilder
question
question  can we write calss A extend StringBuffer?   No you cannot extend
string reverse order
){ String s= " kalins naik" ; StringBuffer buffer = new StringBuffer(s...()){ StringBuffer sb= new StringBuffer(st.nextToken()); System.out.print(" "+sb.reverse
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
Java Interview Questions with Answers
and StringBuffer? What is StringBuilder? How to extract partial characters from
strings
]; } StringBuilder builder=new StringBuilder(reversedSt); builder.insert(0...[] args) { String st="Todays Date is ="; StringBuilder builder=new StringBuilder(st); String str="7/3/2011"; builder.insert(16, str
strings
]; } StringBuilder builder=new StringBuilder(reversedSt); builder.insert(0...[] args) { String st="Todays Date is ="; StringBuilder builder=new StringBuilder(st); String str="7/3/2011"; builder.insert(16, str
(ii) String and String Buffer Class
between String and StringBuffer 1) String object is immutable whereas StringBuffer objects are mutable. 2) String objects are constants
Java - Java Beginners
input=new Scanner(System.in); String str=input.nextLine(); StringBuffer buffer = new StringBuffer(str); StringTokenizer st = new StringTokenizer...(st.hasMoreTokens()){ StringBuffer sb= new StringBuffer(st.nextToken()); System.out.print
In Java I have append to an existing string efficiently
for this? Thanks   Hi, You should use the StringBuilder class for efficiently appending the strings. The StringBuilder class works faster in most
Append Function in Java
  You can use the StringBuffer class in Java to efficiently append... the string is appended to the StringBuffer object you can get the final string through calling the toString() on the StringBufferobject. Use StringBuffer is very
String
) { StringBuilder build = new StringBuilder(); for (int i = 0; i
java
(no); } public String getAlphaNumeric(int len) { StringBuilder sb = new StringBuilder(len); for (int i=0; i int ndx = (int

Ads