Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

StringBuffer class

Posted on 1999-06-29
11
Medium Priority
?
253 Views
Last Modified: 2013-12-29
Ok. In the StringBuffer class we can use the method ensureCapacity() like this:

  StringBuffer Str = new StringBuffer ("ABCDEFGH");
which will allocate the capacity as 8+16 = 24 unicodes.

  Now when you say:
   Str.ensureCapacity(35);
does not gurantee that the capacity would be 35 unicodes. First it will see if 35 or the (current capacity * 2)+2=
24 * 2 + 2 = 50 is the largest and sets to the largest between the two. Therefore in this case the capacity will be 50. Now my question is.... does anyone know why it adds extra 2 unicodes? For examples ... why (current-capacity *2) - which I understand... but why + 2 ?
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 2

Expert Comment

by:jasbro
ID: 1244800
Java doesn't use a null character does it?  I know it keeps track of the length, but maybe it puts two bytes on to hold a unicode null character...don't know for sure, just a random thought.
0
 
LVL 7

Expert Comment

by:vladi21
ID: 1244801
\r,\n ?
:)
0
 

Author Comment

by:prain
ID: 1244802
jasbro: Actually java does not use a null character. Even if it uses a null character why 2? it should have been 1. Here we are talking about Unicodes and not bytes. If the null character was there it would have been 1 unicode = 2 bytes.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:prain
ID: 1244803
valdi21: But still why two types of characters? I know you are :) ing.
0
 
LVL 2

Expert Comment

by:jasbro
ID: 1244804
I didn't think it did, but I couldn't think of anything else.  

vladi21:  Why would they add a \r\n to a String, that would only be added in a print stream if a new line was needed,.
0
 
LVL 7

Accepted Solution

by:
vladi21 earned 20 total points
ID: 1244805
I think:

(z*2+2) mod 4 == 0

int - 4 bytes.
Optimizing memory usage.
0
 
LVL 2

Expert Comment

by:jasbro
ID: 1244806
vladi21, sorry, that isn't correct...in prains example, z=24 so 24*2+2 = 50, and 50 mod 4 = 2
0
 
LVL 2

Expert Comment

by:jasbro
ID: 1244807
It's probably more for small initial buffers.  If the current buffer is 1 or 2, it would make very little sense to only double the buffer size, (if you are ensuring the capacity, you are quite likely going to add more that one or two characters, so doubling and adding two, gives you a few characters to deal with, instead of just one or two more).  As the initial buffer size increases, the extra two characters make very little difference.  Again, this is just a guess, but I can't find any reasoning on it anywhere.
0
 
LVL 2

Expert Comment

by:jasbro
ID: 1244808
Actually, the more I think about my last comment, the more sense it makes, it speeds up the rate at which the buffer grows, so the jvm won't have to increase the buffer size so many times if you try to add a bunch of characters to a very small buffer.
0
 

Author Comment

by:prain
ID: 1244809
Actually, Java StringBuffer is 16 unicodes by default. For example, if you create a blank StringBuffer still the minimum (which is default) buffer size (Capacity) is 16 unicodes (= 32 bytes).

       StringBuffer Str = new StringBuffer();

 Now   Str.Capacity() will be 16. So there is no such thing as buffer size is 1 or 2.
But when a String is added to the buffer, the buffer size (Capacity) will be Str.length + 16. Hence
       Str.append ("Testing");
will give you a string length of 7 and a capacity of 23.

But the whole problem (the +2 business) happens only when you try to change the Capacity (through ensureCapacity().).
0
 
LVL 7

Expert Comment

by:vladi21
ID: 1244810
z=24 unicode chars=48 bytes  so (24*2+2)*2 = 100, and 100 mod 4 = 0
but (24*2)*2=96 and 96 mod 4=0  :)


I think its for some internal optimizing memory usage or data alignment.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question