public class TestString extends java.lang.Object
sprintf(buffer, "text-format", arguments, ...)
.
This approach have some risks which may produce errors in runtime:
buffer
is less, an overflow isn't detect. An overflow may occur, if a value is unexpected,
and the numbering formating produce a less longer output. char*
-Arguments are critically too,
because there length may be rate faulty because that is depending of outer code.
char*
is used as argument, and the pointer is faulty, an unexpected buffer overflow
may be produced in the sprintf
.
sprintf(...)
in C should implement carefully, it is sensitive.
String result = "value=" + value
. This variant needs a buffer,
which is allocated in the heap and managed by garbage collection.
result.setLength(0); result.append("value=").append(value);
The StringBuffer-Object can be allocated as an element of any class, the memory space is allocated
in the instance including the buffer itself, hence no dynamic memory allocation is necessary.
sprintf
approach using String.format(formatString, arguments ...)
.
This method is some more safety as the C-sprintf
, because a buffer overflow is detected and the types
of arguments are well known. But the method works with dynamic memory only.
StringBuffer
is proper. The StringBuffer
can be defined
as embedded Buffer with a fix length inside a class type. Examples using such a fix StringBuffer
allocated in the Stack too are test and shown in the method testStringBuffer()
.
Modifier and Type | Method and Description |
---|---|
java.lang.String |
testStringProcessing()
Calls the test routines.
|
java.lang.String |
toString() |
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String testStringProcessing()