Why does new String("") compile while char c = '' does not?

Java

Java Problem Overview


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 = '';

Java Solutions


Solution 1 - Java

Because char represents a single character, which '' isn't. A String can contain zero or more characters, but a character cannot be anything other than a single character.

Solution 2 - Java

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. But trust me, 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.


One comments on the accepted answer proposes that '' should mean the same as '\0'. But the counter argument to this is that '\0' does not mean no characters. It means one character whose value is zero. So if '' actually means one character it is misleading. And since there is already a way of denoting this ... '\0' ... a second way of denoting it is redundant.

If some piece of new language syntax is both misleading and redundant, it is hard to justify adding it.

In respose to SoloBold's comment, while it would be possible to define a character type to do that, it would require more space. At least 1 bit, and more likely 16 bits to avoid awkward memory alignment problems. A 32-bit char type would not be acceptable ... even though it does solve other problems.

Solution 3 - Java

"" is an empty array of characters.

'' is simply not a character.

Solution 4 - Java

An empty string is like a container that holds nothing. A char must have a value and without exactly one character there is no where to derive that value from.

Solution 5 - Java

You could see a String as a sequence of characters. Having an empty sequence with no characters makes sense, having a character that isn't a character doesn't.

Also never use String s = new String("");, just String s = "" is enough.

Solution 6 - Java

Mathematically, an alphabet A is a set of symbols, suppose alphabet A = {a, b, c, d, ..., z}. A string L is a sequence of zero or more characters from an alphabet, i.e. L = A*. An empty string is simply a sequence of zero characters; while an "empty chars" is not a member of the alphabet.

You can't have an empty character, it is illogical.

Solution 7 - Java

char is a primitive type, so you need to give a value (whatever it is). If you want to leave you variable as "undefined", you can use the wrapper object:

Character c = null;

In this way you variable c is not containing (yet) any value. But then be sure to add a value! :)

Solution 8 - Java

By the way, you can use the object wrapper for primitive char type:

Character c = null;
Character c2 = 'a';

This can be useful when you want a field to be "either a character or nothing".

Solution 9 - Java

String s = null;              // OK
String s = new String("");    // OK
String s = new String("A");   // OK
String s = new String("ABC"); // OK

char c = 'A';                     // OK
char c = '';                      // NOT OK!
Character c = null;               // OK
Character c = new Character('A'); // OK
Character c = new Character('');  // NOT OK!

Solution 10 - Java

Probably because char is a primitive type, and String is an object. boolean, int etc. also don´t allow "empty" values.

Solution 11 - Java

A char is a single character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar.

Solution 12 - Java

String is a set of characters which could be 0 or more. so empty strings are valid- which dont have any character. BUt char represents the primitive type of Character which has to be one valid character, which '' is not.

Solution 13 - Java

i think you should try to know the difference between blank character/string and empty character/string. blank char/string refers to character/string that has some content, such as ' '(SPACE), '\t' and so on, while empty ones doesn't have any content, and can be seen as an empty container.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionblue-skyView Question on Stackoverflow
Solution 1 - JavaMarcelo CantosView Answer on Stackoverflow
Solution 2 - JavaStephen CView Answer on Stackoverflow
Solution 3 - JavahakonView Answer on Stackoverflow
Solution 4 - JavaFlexoView Answer on Stackoverflow
Solution 5 - JavakapexView Answer on Stackoverflow
Solution 6 - JavaLie RyanView Answer on Stackoverflow
Solution 7 - JavaCristiano GhersiView Answer on Stackoverflow
Solution 8 - JavaKosView Answer on Stackoverflow
Solution 9 - JavaGiorgioView Answer on Stackoverflow
Solution 10 - JavaTobiasView Answer on Stackoverflow
Solution 11 - JavaxdazzView Answer on Stackoverflow
Solution 12 - JavaSwagatikaView Answer on Stackoverflow
Solution 13 - JavaBenMiracleView Answer on Stackoverflow