41

I am beginner and self-learning in Java programming. So, I want to know about difference between String.length() and String.getBytes().length in Java.

What is more suitable to check the length of the string?

4
  • What did your experiments suggest? Commented Apr 29, 2013 at 4:04
  • Have you tried using it on a unicode String? Commented Apr 29, 2013 at 4:04
  • 3
    string.length will give the number of characters in a string, where as string.getBytes().length will give number of bytes used by the string to store the value. In normal circumstances each character can take 2 bytes each to store a unicode value Commented Apr 29, 2013 at 4:05
  • 1
    @ArunPJohny: No, string.length will return the number of "Unicode code units" in the string. For ASCII text, it will count characters. For arbitrary Unicode text, it's... complicated. See BeeOnRope's answer for the ugly details. Commented Feb 15, 2016 at 14:29

4 Answers 4

51

String.length()

String.length() is the number of 16-bit UTF-16 code units needed to represent the string. That is, it is the number of char values that are used to represent the string and thus also equal to toCharArray().length. For most characters used in western languages this is typically the same as the number of unicode characters (code points) in the string, but the number of code point will be less than the number of code units if any UTF-16 surrogate pairs are used. Such pairs are needed only to encode characters outside the BMP and are rarely used in most writing (emoji are a common exception).

String.getBytes().length

String.getBytes().length on the other hand is the number of bytes needed to represent your string in the platform's default encoding. For example, if the default encoding was UTF-16 (rare), it would be exactly 2x the value returned by String.length() (since each 16-bit code unit takes 2 bytes to represent). More commonly, your platform encoding will be a multi-byte encoding like UTF-8.

This means the relationship between those two lengths are more complex. For ASCII strings, the two calls will almost always produce the same result (outside of unusual default encodings that don't encode the ASCII subset in 1 byte). Outside of ASCII strings, String.getBytes().length is likely to be longer, as it counts bytes needed to represent the string, while length() counts 2-byte code units.

Which is more suitable?

Usually you'll use String.length() in concert with other string methods that take offsets into the string. E.g., to get the last character, you'd use str.charAt(str.length()-1). You'd only use the getBytes().length if for some reason you were dealing with the array-of-bytes encoding returned by getBytes.

Sign up to request clarification or add additional context in comments.

Comments

9

The length() method returns the length of the string in characters.

Characters may take more than a single byte. The expression String.getBytes().length returns the length of the string in bytes, using the platform's default character set.

1 Comment

There's no such method getLength() I think you mean String.getBytes().length.
3

The String.length() method returns the quantity of symbols in string. While String.getBytes().length() returns number of bytes used to store those symbols. Usually chars are stored in UTF-16 encoding. So it takes 2 bytes to store one char. Check this SO answer out.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.