473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help me understand the java stringbuffer

I am trying to understand what the code below is doing, i know the functions of a string buffer, however am finding it difficult to interprete the aim of the code below. Hope someone can help me?

public string tostring(){
stringBuffer sb=new StringBuffer(28);
sb.append("move [x: ");
sb.append(x);
sb.append("; y: ");
sb.append(y);
sb.append(";val: ");
sb.append(getVal());
sb.append("] ");
return sb.string();
}
Mar 10 '08 #1
4 1546
BigDaddyLH
1,216 Expert 1GB
The code is doing the same as:

Expand|Select|Wrap|Line Numbers
  1. return "move [x: " + x + "; y: " + y + "; val: " + getVal() + "]";
I don't see the advantage to using a StringBuffer in this case.
Mar 10 '08 #2
Thanks, its a lot clearer now.
Mar 10 '08 #3
BigDaddyLH
1,216 Expert 1GB
Thanks, its a lot clearer now.
There are places where using StringBuilder to accumulate a String makes sense -- where you have a loop, for example, or where the code is spread through methods, so you create and pass the StringBuilder around. It has its uses, but your example was too simple.
Mar 10 '08 #4
JosAH
11,448 Expert 8TB
While we're at it, a StringBuffer is to a StringBuilder as a Vector is to an ArrayList.
Use a StringBuilder instead if you have to use a mutable string notion. All those
silly synchronized StringBuffer methods aren't needed most of the time.

kind regards,

Jos
Mar 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: cg_news | last post by:
In short, what I am trying to do is, based on a date, calculate the week of year (as described in ISO 8601), then calculate the first and last date in this week period and return them in the format...
0
by: agntsmyth | last post by:
PasswordField    java/awt/Button    java/awt/TextField    (Ljava/lang/String;I)V  !  " ()Ljava/lang/String; getText % $ java/awt/TextComponent ' ( &...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
1
by: rampage | last post by:
I am trying to make a scrollilng text from right to left (it's working) but it don't animate properly... it's not smooth I'm trying to find a way to make it double buffered but i can't figure it...
1
by: henrymania | last post by:
Am writing a code for database backup....by backupservlet is as given below i get the following exception
4
lifeisgreat20009
by: lifeisgreat20009 | last post by:
This is what i am getting on running the program......... C:\PROGRA~1\Java\JDK15~1.0\bin>javac Editor.java Note: Editor.java uses or overrides a deprecated API. Note: Recompile with...
0
by: abhishekbrave | last post by:
Hi, I have to reuse the code given below for displaying a calendar according to the month and year specified by user ...currently this code is fetching systme date and genrating calendar for that......
4
by: abhishekbrave | last post by:
Below is the java code for getting a calendar for current date. I have to reuse it for the month and year entered by user..* My query is how to pass month and year from user and in which format to...
1
by: khalid galal | last post by:
Hi, i am using NetBeans IDE 6.0.1 and i created a server project and a client project, first i run the server that listens to the port "8221" and waits for clients accessing that port, and when i run...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.