You may assign '\u0000' (or 0).
For this purpose, use Character.MIN_VALUE.
Character ch = Character.MIN_VALUE;
Answer from KV Prajapati on Stack OverflowYou may assign '\u0000' (or 0).
For this purpose, use Character.MIN_VALUE.
Character ch = Character.MIN_VALUE;
char means exactly one character. You can't assign zero characters to this type.
That means that there is no char value for which String.replace(char, char) would return a string with a diffrent length.
A Java String is not an array of characters (nor is it a single char, which is an integral primitive type). It is an Object type, and includes a length.
JLS-10.9. An Array of Characters is Not a String says (in part)
In the Java programming language, unlike C, an array of char is not a
String, and neither aStringnor an array ofcharis terminated by'\u0000'(the NUL character).A
Stringobject is immutable, that is, its contents never change, while an array ofcharhas mutable elements.
I mean in the end each character in the string will be stored as a BYTE, now how a empty string will be stored?
No, each character is 2 bytes. The Java Tutorials: Primitive Data Types says
The
chardata type is a single 16-bit Unicode character. It has a minimum value of'\u0000'(or 0) and a maximum value of'\uffff'(or 65,535 inclusive).
In the case of an empty String there aren't any characters; and an empty array has length 0.
String is certainly backed by a char[] (a field known as value), but that does not under any circumstance imply that a String is exactly equivalent to a char[]. They're two different objects.
Now, with that out of the way, let's reason about what we're expecting with a String of length zero. This is how we determine length:
/**
* Returns the length of this string.
* The length is equal to the number of <a href="Character.html#unicode">Unicode
* code units</a> in the string.
*
* @return the length of the sequence of characters represented by this
* object.
*/
public int length() {
return value.length;
}
The big thing here to note is that if we're creating an empty string, the length of our backing array has to be zero. So, the way that an empty String is created is by providing an empty value array at instantiation.
So i have been willing to learn to code for a long time and i started 4 days ago and im watching some youtube tutorials but my VisualStudio 2012 is typing ' ' instead of '' and it keep giving me Empty character literal error. Any solutions ? ( i already tried to google it but i couldnt understand a word)
So, strings are made up of characters. You can have literals of them both. Double-quotes indicate string literals, and single-quotes indicate character literals.
It's ok to have a zero-length string (""), but a zero-length character doesn't make any sense and isn't valid. This is what the compiler is telling you.
Bonus credit: Try not to use "" for empty strings - use String.Empty instead. It communicates your intent better, and reduces the chance of a space accidentally getting in there via typing error.
Use "" for the empty string. Single quotes need exactly one character between them.
'' doesn't make any sense. Try 'A' or 'z' or '%' or '.' instead.