Can you have a negative string l开发者_JS百科ength? If so, is it the same as null?
No; the length of a string is 0 (empty string, represented as ""
) or higher. And an empty string is not the same as null either (in Java it is not called a null string).
To better understand what null means in Java, you could check out these questions:
- Is null an Object?
- Is null a Java keyword?
No, never.
length is unsigned number, it can't be negative.
the first question's answer negates the second question's legitimacy.
No. String length is any positive integer, or zero. Null is not equal to zero, nor is null equal to any negative value.
String.length()
returns the legth of the string as a primitive int
, so the return value can't be null.
The javadoc doesn't state that the return value can be less than zero, but that the method returns the number of characters in the string.
You cannot construct a valid String with a negative length.
However to answer you question in more depth, the length() method is
public int length() {
return count;
}
and you can use reflection to change the count
to -1 or any negative int
value. This is highly unlikely to be useful but could create some very confusing bugs in your system.
i don't think so except if you deliberately chose to use an offset (like -1) Otherwise No, any empty string is of length 0
精彩评论