String Buffer

String Buffer

In object-oriented programming, a String Buffer is an alternative to String. It has the ability to be altered through adding or appending, whereas a String is normally fixed or unchangeable.

Java's approach

Theory

Java's standard way to handle text is to use its Javadoc:SE|java/lang|String class. Any given String in Java is an immutable object, which means its state cannot be changed. A String has an array of characters. Whenever a String must be manipulated, any changes require the creation of a new String (which, turn, involves the creation of a new array of characters, and copying of the original array). This happens even if the original String's value or intermediate Strings used for the manipulation are not kept.

Java provides an alternate class for string manipulation, called a [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html StringBuffer] (see also StringBuffer and StringBuilder). A StringBuffer, like a String, has an array to hold characters. It, however, is mutable (its state can be altered). Its array of characters is not necessarily completely filled (as oppose to a String, whose array is always the exact required length for its contents). Thus, it has the capability to add, remove, or change its state without creating a new object (and without the creation of a new array, and array copying). The exception to this is when its array is no longer of suitable length to hold its content. In this case, it is required to create a new array, and copy contents.

For these reasons, Java would handle an expression like

String newString = aString + anInt + aChar + aDouble;

like this:

String newString = (new StringBuffer(aString)).append(anInt).append(aChar).append(aDouble)).toString();

Implications

Generally, a StringBuffer is more efficient than a String in string handling. However, this is not necessarily the case, since a StringBuffer will be required to recreate its character array when it runs out of space. Theoretically, this is possible to happen the same number of times as a new String would be required, although this is unlikely (and the programmer can provide length hints to prevent this). Either way, the effect is not noticeable in modern desktop computers.

As well, the shortcomings of arrays are inherent in a StringBuffer. In order to insert or remove characters are arbitrary positions, whole sections of arrays must be moved.

The method by which a StringBuffer is attractive in an environment with low processing power takes this ability by using too much memory, which is likely also at a premium in this environment. This point, however, is trivial, considering the space required for creating many instances of Strings in order to process them. As well, the StringBuffer can be optimized to "waste" as little memory as possible.

See also

* The JavaDocs of [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html StringBuffer] , [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html StringBuilder] and [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html String] , as well as the article StringBuffer and StringBuilder.
* The source code of these classes
* [http://www-128.ibm.com/developerworks/java/library/j-jtp04223.html?ca=dgr-lnxw01JavaUrbanLegends Urban Performance Legends] - An article which involves a discussion of immutable objects with respect to object-oriented design
* Analysis of algorithms


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Buffer overflow — In computer security and programming, a buffer overflow, or buffer overrun, is an anomalous condition where a process attempts to store data beyond the boundaries of a fixed length buffer. The result is that the extra data overwrites adjacent… …   Wikipedia

  • Buffer overflow protection — refers to various techniques used during software development to enhance the security of executable programs by detecting buffer overflows on stack allocated variables as they occur and preventing them from becoming serious security… …   Wikipedia

  • String.h — Saltar a navegación, búsqueda string.h es un archivo de la Biblioteca estándar del lenguaje de programación C que contiene la definición de macros, constantes, funciones y tipos de utilidad para trabajar con cadenas de caracteres y algunas… …   Wikipedia Español

  • string.h — es un archivo de la Biblioteca estándar del lenguaje de programación C que contiene la definición de macros, constantes, funciones y tipos de utilidad para trabajar con cadenas de caracteres y algunas operaciones de manipulación de memoria (el… …   Wikipedia Español

  • String.h — is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used not only for string handling but also various memory handling functions; the name is… …   Wikipedia

  • String (computer science) — In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet. In computer programming, a string is traditionally a sequence of… …   Wikipedia

  • String exploits — Several implementation / design flaws are associated with string programming, some of those are associated with security exploits. Concatenation problems It is possible to cause String1 + User Input String + String2 to behave in unepected ways by …   Wikipedia

  • Buffer Overflow — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Buffer overflow — Dépassement de tampon En informatique, un dépassement de tampon ou débordement de tampon (en anglais, buffer overflow) est un bogue causé par un processus qui, lors de l écriture dans un tampon, écrit à l extérieur de l espace alloué au tampon,… …   Wikipédia en Français

  • Buffer-Overflow — Pufferüberläufe (engl. buffer overflow) gehören zu den häufigsten Sicherheitslücken in aktueller Software, die sich u. a. über das Internet ausnutzen lassen können. Im Wesentlichen werden bei einem Pufferüberlauf durch Fehler im Programm zu große …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”