Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null termination in strings

Tags:

java

c++

c

string

Yes, I did check other threads and I have come to a conclusion. I just want you to confirm it so that I don't have any misconceptions.

Java String objects are not null terminated.

C++ std::string objects are also not null terminated

C strings or C-style strings if you will (array of characters), are the only strings that are null-terminated.

Correct or Incorrect?

like image 268
B-Mac Avatar asked Feb 23 '26 23:02

B-Mac


1 Answers

C-strings are 0-terminated strings. You aren't forced to use them in C though.

Both C++ std::string and Java strings are counted strings, which means they store their length.

But C++ std::strings are also followed by a 0 since C++11, making them 0-terminated if (as often the case) they don't contain any embeddded 0, for better interoperability with 0-terminated-string APIs.

like image 98
Deduplicator Avatar answered Feb 26 '26 11:02

Deduplicator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!