Videos
Can I calculate string length Java without using .length()?
How does null handling affect string length Java operations?
Are arrays and strings interchangeable when checking length in Java?
The length is just text.length(). Checking for a \0 terminator is a C-ism. Java doesn't terminate strings with NUL.
As stated in this SO,
Strings in Java do NOT have a NULL terminator as in C, so you need to use the length() method to find out how long a string is.
Therefore you can change the condition to
while (i < text.length())
(If your goal really is to get the length of a string, your function therefore is redundant, as it is already built in)
Hi, I've been trying to find a name for my method that returns the length of an object. A quick google search showed that it's conventional to name getters getX(). However, when I investigated the first standard class that came to mind - String, I found it having the length() method instead of expected getLength(). Why is that? How should I name a length method in my own class then?
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--