difference between Strings in C++ and Java

In C++ I can do something like this...

String s = "abc";
char c = s[i]; // works fine...

But in Java, if I try doing the same, it throws an error. Why?.

In java, to achieve the above, I have to do :

s.toCharArray(); 

How is the implementation of Strings in C++ different from that in Java?

Jon Skeet
people
quotationmark

In java, to achieve the above, I have to do : s.toCharArray();

Not really. You can use charAt instead:

char c = s.charAt(i);

Basically, C++ allows user-defined operators - Java doesn't. So the String class doesn't expose any sort of "indexing" operator; that only exists for arrays, and a String isn't an array. (It's usually implemented using an array, but that's a different matter.)

EDIT: As noted in comments, the + operator is special-cased for strings - right in the language specification. The same could have been done for [], but it isn't - and as it's not in the language specification, and Java doesn't support overloaded operators, it can't be performed in library code. (For example, you can't give custom behaviour to + for any other class.)

people

See more on this question at Stackoverflow