在我的方法的实际参数中传递短数据类型时出错
Error when I pass short data type in the acutal parameters of my method
public static void example(short a, int b, int c){
System.out.println("example");
}
public static void main(String[]args){
example(1,2,3); /*I'm getting a compile time error "the method
example(short,int,int) is not applicable for the
arguments(int, int, int)*/
我确实通过在形式参数中将所有值声明为 int 数据类型来修复它,但是
1 不是短数据类型吗??我只想知道为什么我不能在实参中传1。
我正在使用 Java 顺便说一句
你没有说这是什么编程语言,所以无法确定,但是从错误消息中可以清楚地看到:
isn't 1 a short data type??
为假。
从报错信息中不难看出,不,1
是不是表示short
的字面量,实际上是表示 int
.
的文字
同样,您没有说明您使用的是什么语言,因此无法绝对确定是否是这种情况,但从错误消息来看,它肯定是这样的。例如,如果您查看 Java,Java Language Specification 明确表示:
An integer literal is of type long
if it is suffixed with an ASCII letter L
or l
(ell); otherwise it is of type int
(§4.2.1).
因此,如果您的代码片段中的语言是 Java,那么 1
将是 int
。
public static void example(short a, int b, int c){
System.out.println("example");
}
public static void main(String[]args){
example(1,2,3); /*I'm getting a compile time error "the method
example(short,int,int) is not applicable for the
arguments(int, int, int)*/
我确实通过在形式参数中将所有值声明为 int 数据类型来修复它,但是 1 不是短数据类型吗??我只想知道为什么我不能在实参中传1。 我正在使用 Java 顺便说一句
你没有说这是什么编程语言,所以无法确定,但是从错误消息中可以清楚地看到:
isn't 1 a short data type??
为假。
从报错信息中不难看出,不,1
是不是表示short
的字面量,实际上是表示 int
.
同样,您没有说明您使用的是什么语言,因此无法绝对确定是否是这种情况,但从错误消息来看,它肯定是这样的。例如,如果您查看 Java,Java Language Specification 明确表示:
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell); otherwise it is of typeint
(§4.2.1).
因此,如果您的代码片段中的语言是 Java,那么 1
将是 int
。