Start of Tutorial > Start of Trail > Start of Lesson | Search |
By convention, C and C++ strings are null-terminated array of characters; there is no real entity in C and C++ that is a string. Java strings are first-class objects.Strings as objects provides several advantages to the programmer:
To illustrate why this is an important feature of Java, let's look at a small example. This C function copies the contents of
- The manner in which you obtain strings and elements of strings is consistent across all strings and all systems.
- Since the programming interface for the
String
andStringBuffer
classes is well-defined, Java strings function predictably every time.- The
String
andStringBuffer
classes do extensive runtime checking for boundary conditions. They catch errors for you.src
intodest
.int myStrCopy(char *dest, char *src) { for ( ; *src != '\0'; src++, dest++) *dest = *src; }
String
s Behave UnpredictablyIn the example shown above, the developer uses pointer arithmetic to step through both strings copying one into the other. While allowing programmers to inspect arbitrary memory locations through pointers is a powerful tool, this power can be the source of many errors. One fruitful source of errors is pointers that stray off the end of an array. ThemyStrCopy
function above has such an error: thefor
loop in the function does not check the length ofdest
, and ifsrc
is longer thandest
the string copy writes right over the end ofdest
. Here's a program that tickles the bug.On my machine, the program prints:main() { char *s = "HotJava is Cool!"; char t[] = "Java is Cool!"; printf("%s, %s\n", s, t); myStrCopy(t, s); printf("%s, %s\n", s, t); }HotJava is Cool!, HotJava is Cool!%s, %smyStrCopy
writes over the end ofdest
thereby corrupting whatever was stored in the memory after it. Note:%s, %s
are the characters that happened to be stored in the memory location afterdest
and will probably be different when you run the program on your machine.Sure, the error in
myStrCopy
can be fixed easily. But errors like this are often difficult to find.
Java strings are first-class objects deriving either from theString
class or theStringBuffer
class. This makes finding and fixing an entire class of common and frustrating programming errors such as the one illustrated above trivial.Here's the program above (including the error) rewritten in the Java language:
Notice that this translation uses theclass strcpy { public static void main(String[] args) { String s = "HotJava is Cool!"; StringBuffer t = new StringBuffer("Java is Cool!"); System.out.println(s + ", " + t); myStrCopy(t, s); System.out.println(s + ", " + t); } static void myStrCopy(StringBuffer dest, String src) { int i, len = src.length(); for (i = 0; i < len; i++) dest.setCharAt(i, src.charAt(i)); } }String
class, theStringBuffer
class, and the methods appropriate for obtaining specific characters instead of character arrays and pointers.Like the C version, the Java language version of the
myStrCopy
method loops over the length ofsrc
and never checks the length ofdest
. Thus, whensrc
is longer thandest
, the method tries to obtain characters beyond the end ofdest
. However, when you run the Java language version, you'll see the following runtime error message:The primary difference between the Java language version of this program and the C version is that the Java program will reliably and obviously crash, whereas the C program will do something obscure.Exception in thread "main" java.lang.StringIndexOutOfRangeException String index out of range: 13 at java.lang.Exception.< init >(Exception.java) at java.lang.StringIndexOutOfRangeException.< init >(StringIndexOutOfRangeException.java) at java.lang.StringBuffer.setCharAt(StringBuffer.java) at strcpy.myStrCopy(strcpy.java:23) at strcpy.main(strcpy.java:15)
Start of Tutorial > Start of Trail > Start of Lesson | Search |