I know about C and I am entering into Java and confused about its approach towards arrays and strings. It’s totally different from arrays and strings in C. Please help me understand what is actually the difference between C and Java (for strings and arrays).
Share
In C, a string is typically just an array of (or a pointer to) chars, terminated with a NUL (\0) character. You can process a string as you would process any array.
In Java, however, strings are not arrays. Java strings are instances (objects) of the
java.lang.Stringclass. They represent character data, but the internal implementation is not exposed to the programmer. You cannot treat them as arrays, although, if required, you can extract string data as an array of bytes or chars (methodsgetBytesandgetChars). Note also that Java chars are 16-bits, always, while chars in C are typically (not always) 8-bit.