Why does Java require char variables to use single quotes?

Why does Java require char variables to contain anything in single quotes instead of double? An example:

char s = 's'; //This will not break

VS:

char s = "s"; //This will break

I understand that double quotation marks are mainly used for strings, but there is a specific reason why characters can only use single quotation marks and break when entering double quotation marks?

I tried to do some research, but the only relevant is another so question, which refers to the difference between single quotation marks and double quotation marks, not the reason behind the development of char grammar

resolvent

Solution

Because char and string are two different types, Java should know whether you want a character or string with a length of 1?

For example, given the method:

public void foo(String s) { }
public void foo(char c) { }

And telephone

foo("a");

If characters can be made in double quotes, how does Java know which method to call?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>