This class contains some examples to test String functionality.
Situation for String preparation in C:
A String preparation in C in embedded applications is often necessary in situations of error reporting
or output of states. The typically choice is usage of
sprintf(buffer, "text-format", arguments, ...)
This approach have some risks which may produce errors in runtime:
- The type of arguments should be matched to the designation of parts in text-format.
An error of detail is possible, the user should programmed carefully. The compiler doesn't detect errors.
- If the size of
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.
- If a
char* is used as argument, and the pointer is faulty, an unexpected buffer overflow
may be produced in the
- The buffer should be provided by the users environment. If the buffer is defined in Stack,
and the String is produced and used temporary, it's okay. But a software correction in calling routines,
which doesn't uses to pointer to the buffer for copy, instead save the pointer, are not detected at compile time.
The usage of
in C should implement carefully, it is sensitive.
The usage of Strings in Java is some more unsensitive and simple. But the standard-Java uses the system of garbage collection
to accomplish this requirements. This may not able to use in all situations of embedded control-software.
The preparation of Strings may realized in Java in three ways:
- Simple concatenations of Strings just like
String result = "value=" + value. This variant needs a buffer,
which is allocated in the heap and managed by garbage collection.
- Usage of a StringBuffer or StringBuilder.
The adequate code is
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.
- Java knows a adequate to
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.
If embedded software written in Java and translated to C shouldn't used dynamic memory and garbage collection,
the second approach using
is proper. The
can be defined
as embedded Buffer with a fix length inside a class type. Examples using such a fix
allocated in the Stack too are test and shown in the method
If the garbage collector is able to use, a String concatenation is able to use. The user doesn't need to
provide any buffers.