3
   

Basic Java String questions

 
 
jddev
 
Reply Wed 30 Mar, 2005 09:10 am
Hey,

I'm just learning Java, but from my background in C/C++ I have a question.

Won't this example potentially produce unexpected results later on in the program?
Code:
StringBuffer s3 = new StringBuffer(2);
s3.insert(0, "Hello");
System.out.println(s3);


Since the buffer was initially set to 2 characters and what is being inserted into it is 5 characters, is it possible that the memory holding those extra 3 characters will be overwritten? Or does Java just change the size of the buffer to hold the 5 characters?

In C a string is terminated with a \o if I remember correctly. So "Hello" actually would take 6 characters, not 5. Is this true of Java also?

Thanks,
Jason
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 3 • Views: 7,626 • Replies: 4
No top replies

 
jddev
 
  1  
Reply Wed 30 Mar, 2005 11:34 am
Hey,

Using the capacity() class method, I found that the StringBuffer class dynamically increases the "capacity" of the object. The strange thing is that it increases it to an almost random size.

So,

Code:
import java.text.*;

public class test
{
public static void main(String[] args)
{
StringBuffer s1 = new StringBuffer(5);
StringBuffer s2 = new StringBuffer(10);
StringBuffer s3 = new StringBuffer(16);

System.out.println("s1.capacity = " + s1.capacity());
System.out.println("s1.length = "+ s1.length());
System.out.println("s2.capacity = " + s2.capacity());
System.out.println("s2.length = "+ s2.length());
System.out.println("s3.capacity = " + s3.capacity());
System.out.println("s3.length = "+ s3.length() + "\n");

s1.insert(0, "Hello from Java!");
s2.insert(0, "Hello from Java!");
s3.insert(0, "Hello from Java!");

System.out.println("s1 = " + s1 + "\ns1.capacity = " + s1.capacity() + "\ns1.length = " + s1.length());
System.out.println("s2 = " + s2 + "\ns2.capacity = " + s2.capacity() + "\ns2.length = " + s2.length());
System.out.println("s3 = " + s3 + "\ns3.capacity = " + s3.capacity() + "\ns3.length = " + s3.length());
}
}


Produces:
Quote:

s1.capacity = 5
s1.length = 0
s2.capacity = 10
s2.length = 0
s3.capacity = 16
s3.length = 0

s1 = Hello from Java!
s1.capacity = 16
s1.length = 16
s2 = Hello from Java!
s2.capacity = 22
s2.length = 16
s3 = Hello from Java!
s3.capacity = 16
s3.length = 16


Any ideas on why the capacity for the 2nd one increased to 22? It seems rather pointless. Why not just increase to what is needed? (16 in this case).

Jason
0 Replies
 
FreeDuck
 
  1  
Reply Wed 30 Mar, 2005 12:00 pm
That's a very interesting question. I didn't know the answer so I copied your code and played around with it. It appears to me that there is a formula for the increase in capacity. Guessing from the results I got, I'd say that the capacity increases to 2*original capacity +2, unless the new string exceeds that formula in which case it expands to the size of the new string. For instance, if you insert characters for string 2 until the string is 21 characters long or more, you will see the capacity adjust to exactly the length.
0 Replies
 
jddev
 
  1  
Reply Wed 30 Mar, 2005 02:46 pm
Hey,

Thanks for your response. It seemed quite odd to me that it behaves like you describe, although it quite obviously does. This got me going again, and I found some more information on it on a Java Forum on Sun's website:
Sun's Java Forum

It seems it doubles itself for speed purposes. It consumes more space, but is able to allocate the extra space needed much faster than trying to calculate only what it needs.

Jason
0 Replies
 
FreeDuck
 
  1  
Reply Wed 30 Mar, 2005 04:08 pm
That makes sense that it would be that way for speed.

In 1.5 now they are suggesting that you use StringBuilder instead of StringBuffer. I have only limited experience with 1.5 but I wonder if that doesn't somehow make all of this unnecessary. You've sparked my curiosity so I'm off to check that out.

<correction: I think I meant to say 22 characters or more on my previous post, but you obviously understood what I meant.>
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » Basic Java String questions
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/18/2024 at 02:53:21