为什么Java要求char变量使用单引号? [英] Why does Java require the char variable to use single quotes?

查看:129
本文介绍了为什么Java要求char变量使用单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java要求char变量用单引号而不是double引用其中的任何内容?例如:

  char s ='s'; //这不会打破

vs:

  char s =s; //这将打破

我知道双引号主要用于字符串,但是有一个特定的原因,如为什么chars被构建为只使用单引号并在输入双引号时中断?



我试图做一些研究,但唯一相关的是另一个问题是问题单引号和双引号之间的区别,而不是char的语法开发背后的原因。

解决方案

因为 char String 是两种不同的类型,Java应该知道你是否想要一个长度为1的字符或字符串? / p>

例如,给定方法:

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

电话

  foo(a); 

如果字符可以用双引号制作,那么Java如何知道要调用哪种方法?


Why is it that Java requires the char variable to enclose anything inside of it with single quotes rather than double? An example of this:

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

vs:

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

I understand that double quotes are primarily for strings but is there a specific reason as to why chars were built to only use single quotes and to break when a double quote is entered?

I have tried to do some research but the only thing that was relevant was another SO Question, which refers to the difference between single and double quotes, not the reasoning behind the syntactical development of the char.

解决方案

Because char and String are two different types, and how else is Java supposed to know whether you want a character or string of length 1?

For example, given the methods:

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

And the call

foo("a");

If characters could be made with double quotes, how would Java know which method to call?

这篇关于为什么Java要求char变量使用单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆