Strings Java to C++
Strings in Java are handled in a uniform way using the String class. There are at least three ways in which a string variable can be declared in C++. The all behave in a similar but different fashion.



*[2] means the file "" at line 2.

#include <string>

To work with strings in Java[5] we simply use the String class directly. The String class is automatically imported. In C++ the string class is not capitalized and is not automatically imported. We must include its declarations as in C++[2].


In C++[7] the char type is used to represent a single character. This corresponds to Java's char type except that in Java a single character is stored in Unicode using at least 16 bits. In C++ a single character is stored in ASCII using 8 bits or a single byte.

The char* syntax in C++[7] indicates that the variable sp is a pointer to a character. Pointers are discussed in greater depth on the pointers page. C and C++ work very hard to expose the memory layout of items that are stored. A pointer to a single character and a pointer to an array of characters. Thus the variable sp can be initialized to a whole string of characters. The variable sp is the same pointer. This can be a problem in C++ because a pointer can point to any number of things and it it up to the programmer to manage what the destination is. In Java the type checking is done for you.

char sa[]

This C++[10] syntax declares sa to be an array of characters. It turns out that a pointer to an array of characters is the same as a pointer to a character. Thus in C++ variable sa and sp behave identically even though they are declared as two different things. In Java things are declared according to their behavior and purpose. In C++ they are declared according to how they are actually laid out in memory.

The string "string" in C++[10] is actually 7 characters long instead of 6. In C++ every string is terminated by a null or zero character.

In C++[13-14] the string sa is printed out one character at a time. Note that in C++[13] the for loop continues as long as sa[i] is not equal to 0. The length of the string is not actually stored. You find the length when you reach the zero character. This code behaves exactly the same as the Java[11-12] code.

string objects

As in Java[14], C++[16] provides a string class. (Note that string is not capitalized in C++[16]). Many of the same methods exist in both classes with slightly different names. Note also that looping through the characters in a string in Java[16] is identical to how it is done in C++[18]. Do not confuse string objects with char arrays or char pointers. They are very different things.

Note that to access the individual characters in a string, Java[17] uses the cumbersome charAt() method while C++[19] uses an index, just like an array. This is because in C++ it is possible to override operators like [] to give them a meaning for that class.

Similarly when comparing two strings in Java[21] the very cumbersome compareTo() method is used while in C++[23] the relational operators <, <=, >, >= have been defined on strings. Though not shown in the example code the Java method equals() has been replaced in C++ with the much nicer == operator.

String concatonation

Appending two strings together in Java[26] uses the simple + operator. In C++ the + operator can be used, but the set of cases where it works with various types is more limited. The append() methos can also be used as at C++[28].

Escape characters

All of the escape characters in C and C++[7,14] strings such as \n=new line, \r = return, \\ = backslash, \t = tab, etc. were copied into Java[5,12] and behave exactly the same.