I am beginner and self-learning in Java programming.
So, I want to know about difference between String.length()
and String.getBytes().length
in Ja
In short, String.length()
returns the number of characters in the string while String.getBytes()
.length returns the number of bytes to represent the characters in the string with specified encoding.
In many cases, String.length()
will have the same value as String.getBytes().length
. But in cases like encoding UTF-8 and the character has value over 127, String.length()
will not be the same as String.getBytes().length
.
Here is an example which explains how characters in string is converted to bytes when calling String.getBytes()
. This should give you a sense of the difference between String.length()
and String.getBytes().length
.