What is the use of StringBuilder in java?

0 votes

Is it mandatory to use StringBuilder for string concatenation in java? Is it so?

Jun 19, 2018 in Java by scarlett
• 1,280 points
60 views

1 answer to this question.

0 votes

If you use String concatenation in a loop, like

String str = "";
for (int i = 0; i < 50; i++)
{
    str += ", " + i;
}

then StringBuilder is used (not StringBuffer) instead of a String because it is much faster and consumes less memory.

If you have a single statement,

String str = "1, " + "2, " + "3, " + "4," + "5, " ...;

then you can use Strings because the compiler will use StringBuilder automatically.

answered Jun 19, 2018 by Akrati
• 960 points

Related Questions In Java

0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,040 points
309 views
0 votes
1 answer

What is the use of @Override annotation in Java ? When do we use it ?

I think it is most useful as ...READ MORE

answered May 4, 2018 in Java by Akrati
• 3,150 points
70 views
0 votes
2 answers

What is the use of final class in java?

In Java, items with the final modifier cannot be ...READ MORE

answered Oct 5, 2018 in Java by Daisy
• 8,040 points
36 views
0 votes
1 answer

What is the use of assert keyword in java?

Java assertion feature allows the developer to ...READ MORE

answered Jun 28, 2018 in Java by scarlett
• 1,280 points
35 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 6,200 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
1,632 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
4,338 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
335 views
0 votes
1 answer

What is the difference between Type List and type ArrayList in Java

Always TypeList is preferred over ArrayList. The ...READ MORE

answered Jul 9, 2018 in Java by Akrati
• 960 points
45 views
0 votes
1 answer

What does NaN mean in Java?

"NaN" stands for "not a number". "Nan" ...READ MORE

answered Jul 12, 2018 in Java by Akrati
• 960 points
80 views