implement length() of string without using Built-in functions [closed]

a) You can serialize the string and examine the bytes.(although some other class will call a String’s function.)

b) you can sent it to a Writer subclass where you implement the write(char[], int, int) (although, again, some other class will call a String’s function.)

c) you can try to cheat and use reflexion to pull private fields, and setAccessible(true) on them, to pull the relevant fields. Watchout, this is JRE specific, so not portable across java version.

Leave a Comment