Why are empty Strings valid and empty chars are not ? I would have thought an empty String is not a string but just a placeholder. The same for a char, but creating an empty char does not even compile.
What im wondering is why the following occurs - Compiles -
String s = "";
Does not compile -
char c = '';
Sol :
Because a string literal represents a String which may consist of zero or more characters, but a (valid) character literal represents exactly one character.
A char could be defined as a datatype that can store 0 or 1 characters ...
Yes. In theory it could have been defined that way. If you think through all the issues (e.g. how you'd represent an empty char, how an application would deal with it, etc) you will conclude that the there are few benefits, and significant downsides.
Anyway, doing this merely so that there was a syntactic consistency between String and char literals would be totally crazy. You don't break a language's performance and/or semantics so that the syntax looks nice.
Refer : StackOverflow for full discussion
No comments:
Post a Comment