How the Java Language Differs from C and C++ |
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 the Java language, 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 and StringBuffer classes is well-defined, Java Strings function predictably every time
- the String and StringBuffer class does extensive runtime checking for boundary conditions and catches errors for you
str1
intostr2
.int myStrCopy(char *str1, char *str2) { for ( ; *str1 != '\0'; str1++, str2++) *str2 = *str1; }C Strings Behave Unpredictably
In 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 ofstr2
, and ifstr1
is longer thanstr2
the string copy writes right over the end ofstr2
. 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(s, t); printf("%s, %s\n", s, t); }HotJava is Cool!, HotJava is Cool!%s, %smyStrCopy
writes over the end ofstr2
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 afterstr2
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 Predictable
Java strings are first-class objects deriving either from the String class or the StringBuffer 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 the String class, the StringBuffer class and the methods appropriate for obtaining specific characters instead of character arrays and pointers.class 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(s, t); System.out.println(s + ", " + t); } static void myStrCopy(String str1, StringBuffer str2) { int i, len = str1.length(); for (i = 0; i < len; i++) str2.setCharAt(i, str1.charAt(i)); } }Like the C version, the Java language version of the
myStrCopy
method loops over the length ofstr1
and never checks the length ofstr2
. Thus, whenstr1
is longer thanstr2
, the method tries to obtain characters beyond the end ofstr2
. 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)
How the Java Language Differs from C and C++ |