Question

In: Computer Science

When should you use the String Class as opposed to the StringBuilder Class? Describe why this...

When should you use the String Class as opposed to the StringBuilder Class? Describe why this answer is logical.

Solutions

Expert Solution

Solution

===================

String :- String are immutable objects,Immutable objects are not allowing modifications over their content, if we are trying to perform modifications over immutable object content then operations are allowed , but, the resultant data will not be stored back in original object, where the resultant data will be stored by creating new object.

Where as StringBuilder are mutable i.e modification is allowed over the object.

Performance wise StringBuilder is very fast and consumes less memory than String object while performing modification or concatenation operation.

for Exaple:-

String str = "ABC";

str = str+"BAC";

here two object "ABC" and "ABCBAC" is created

StringBuilder sb=new StringBuilder("ABC");

sb.append("BAC");

Here only one object is created "ABCBAC"

Conclusion:-

A String can be used when we don't change String object once created or concatenation operation is not required. A StringBuilder can be used when we have to change Object frequently and high perfromance is is needed.

For Example In application We can use String object for non changing field like user id, where as we can use StringBuilder in those field which changes like destination of employee


Related Solutions

You are given a string literal “CS3360” Describe what is Stringbuilder class, 3 member methods in...
You are given a string literal “CS3360” Describe what is Stringbuilder class, 3 member methods in Stringbuilder class and use the above literal to show output of 3 methods.
You coded the following class: public class N extends String, Integer { } When you compile,...
You coded the following class: public class N extends String, Integer { } When you compile, you get the following message: N.java:1: ‘{‘ expected public class N extends String, Integer                                                ^ 1 error Explain what the problem is and how to fix it.
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must...
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must use var-arg concept.   Class name for this program is Palindrome.   Must pass this test:: public class PalindromeTest { public static void main(String[] args) { Palindrome palindrome = new Palindrome(); boolean answer = palindrome.pCheck("racecar", "Bell", "elle", "bunny"); System.out.println(“These are palindromes: " + answer); } }
Explain why there is more danger when you fall and bounce as opposed to falling without...
Explain why there is more danger when you fall and bounce as opposed to falling without bouncing.
Synthesis of Acetaminophen A) Why should you use a minimum amount of a boiling solvent when...
Synthesis of Acetaminophen A) Why should you use a minimum amount of a boiling solvent when recrystallizing the crystals of the acetaminophen? Explain. B) Why should you use a minimum amount of a very cold solvent when washing the crystals of the acetaminophen? Explain.
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write...
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or...
Explain why a common stock should be evaluated in a portfolio context as opposed to being...
Explain why a common stock should be evaluated in a portfolio context as opposed to being evaluated in isolation.
1.) In the U.S. when does a company use the current rate method as opposed to...
1.) In the U.S. when does a company use the current rate method as opposed to the temporal method? 2.) What is an interest rate swap? Please help me answer these international financial management questions. Thank you!
1. What is a copyright image? 2. Why should you not use copyright images when designing?...
1. What is a copyright image? 2. Why should you not use copyright images when designing? 3. Why is it not okay to modify copyright work to make your own? 4. Why do some companies put watermarks on their work? 5. Why legal ramifications can result from using a copyright image? 6. What risks can a designer run into when just doing an image search on a search engine? 7. How can using someone else work, hurt the artist that...
Explain why companies should discount projects using the cost of equity. When should they use the...
Explain why companies should discount projects using the cost of equity. When should they use the WACC instead? When should they use either?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT