There are a few noteworthy differences between Java and C++ strings (C strings will be less mentioned as they are quite outdated and more awkward to manipulate).

In Java, a string is nothing more than a sequence of characters.

Thus whilst it can be tempting to create them using the primitive data type char, this makes life unnecessarily difficult as there is a predefined String object.

When using Strings, you cannot write

as the square bracket notation only works for arrays.

The correct way to write this is

The additional line at the end is incorrect and illustrates how strings shouldnt be viewed as an array of characters.

So how does the Java interpreter view them?

Consider

str1 is a pointer pointing to a string literal.

When the value of str1 is assigned to str2, a pointer is assigned, i.e. str1 and str2 point to the same string literal.

What happens at the last line? One might suppose the chunk of memory housing the string literal today now houses instead tomorrow, and both str1 and str2 point to tomorrow.

Wrong.

str1, in fact, now points to a new address housing tomorrow. Meanwhile str2 continues pointing to the same address housing today.

Why is this? The String object is immutable. Once created at an address, it cannot be modified (however, if the number of variables pointing to that address drops to zero, then the memory is reclaimed or “garbage collected”).

Hence all predefined methods on the String object leave the receiver object unchanged. If the method needs to make changes, a new string is created and returned.

Consequently, it is not possible to write a function that reverses a string in-place in Java. You can, however, write such a function for an array as they are mutable. When reversing a string, the result must be returned

Do strings work the same way in C++?

Not exactly

Using square brackets is OK.

We can also use

which is less intuitive but has the advantage of throwing an exception if the index supplied is out of range.

Viewing a string as an array of characters works and strings are mutable

Reminder: C++ objects are passed by value and not reference like in Java.

Strings then seem easier to work with in C++ than Java.

However, whilst

in Java displays hello, goodbye as expected, in C++

errors.

The overloaded operator +` expects C++ string objects as arguments.