这可能是有史以来在堆栈溢出中提出的最愚蠢的问题,但这很困扰我...
public class shorte { public static void main(String []args) { short e = 56; // no need for explicit cast System.out.println(e); start(56); // why does int literal here needs explicit cast ... } static void start(short e) { System.out.println(e); } }虽然从函数中的int文字创建正常的短变量没有要求进行任何显式转换,但为什么将int文本传递给short变量(参数传递)需要显式转换...... ??
然后再次
我现在不建议在单个帖子中提出两个不相关的问题,但这在另一篇文章中提出这个问题太微不足道了。
**'for'计数器变量的范围**
public class forloop { public static void main(String []args) { int a =12; for(int a =12;a<14;++a) // no showdowing of variable ,give compile-time error { System.out.println(a); } } }所以我用普通块试了一下
a=12 { a=13; even this doesn't compile }这是否意味着,块没有自己的范围......
this is may be the dumbest question ever asked in stack overflow but this is bothering me lot...
public class shorte { public static void main(String []args) { short e = 56; // no need for explicit cast System.out.println(e); start(56); // why does int literal here needs explicit cast ... } static void start(short e) { System.out.println(e); } }while creating normal short variable from an int literal in function didn't ask for any explicit cast , but why does passing int literal to short variable (parameter passing) needs an explicit cast......??
and again
i now it is not recommended to ask two unrelated questions in single post but this too trivial to ask in yet another post..
**scope of 'for' counter variable **
public class forloop { public static void main(String []args) { int a =12; for(int a =12;a<14;++a) // no showdowing of variable ,give compile-time error { System.out.println(a); } } }so i tried it with normal blocks
a=12 { a=13; even this doesn't compile }does that mean, blocks don't have their own scope...
最满意答案
一个short文字可以从Java中的int文字自动构建。 这是的情况
short e=50;但是调用一个函数并传递参数是不同的。 由于重载的可能性,函数调用应该与函数原型完全匹配。 想象一个重载函数,一个版本的int参数和另一个版本的short参数。
至于第二个问题,在大多数语言中,通常不能通过内部作用域中的相同标识符隐藏在外部作用域中声明的标识符。 我头脑中有两个例外:
1-全局变量可以被局部变量隐藏(例如在C ++中,因为Java没有全局变量)。
2-类数据成员可以被局部变量隐藏。
在这两种情况下,该语言都提供了一个解析运算符(:: for 1-和this for 2-)。
A short literal can be automatically constructed from an int literal in Java. This is the case of
short e=50;But calling a function and passing parameters is different. Because of the possibility of overloading, the function call should exactly match the function prototype. Imagine an overloaded function with an int parameter for one version and a short parameter for another.
As for the second question, in most languages, you usually cannot hide an identifier declared in an outer scope by the same identifier in an inner scope. I have two exceptions in my head:
1- A global variable can be hidden by a local variable (in C++ for example, as Java does not have global variables).
2- A class data member can be hidden by a local variable.
In both cases, the language offers a resolution operator (:: for 1- and this for 2- ).
将int literal传递给在java中使用整数的方法(passing int literal to method that takes integer in java)这可能是有史以来在堆栈溢出中提出的最愚蠢的问题,但这很困扰我...
public class shorte { public static void main(String []args) { short e = 56; // no need for explicit cast System.out.println(e); start(56); // why does int literal here needs explicit cast ... } static void start(short e) { System.out.println(e); } }虽然从函数中的int文字创建正常的短变量没有要求进行任何显式转换,但为什么将int文本传递给short变量(参数传递)需要显式转换...... ??
然后再次
我现在不建议在单个帖子中提出两个不相关的问题,但这在另一篇文章中提出这个问题太微不足道了。
**'for'计数器变量的范围**
public class forloop { public static void main(String []args) { int a =12; for(int a =12;a<14;++a) // no showdowing of variable ,give compile-time error { System.out.println(a); } } }所以我用普通块试了一下
a=12 { a=13; even this doesn't compile }这是否意味着,块没有自己的范围......
this is may be the dumbest question ever asked in stack overflow but this is bothering me lot...
public class shorte { public static void main(String []args) { short e = 56; // no need for explicit cast System.out.println(e); start(56); // why does int literal here needs explicit cast ... } static void start(short e) { System.out.println(e); } }while creating normal short variable from an int literal in function didn't ask for any explicit cast , but why does passing int literal to short variable (parameter passing) needs an explicit cast......??
and again
i now it is not recommended to ask two unrelated questions in single post but this too trivial to ask in yet another post..
**scope of 'for' counter variable **
public class forloop { public static void main(String []args) { int a =12; for(int a =12;a<14;++a) // no showdowing of variable ,give compile-time error { System.out.println(a); } } }so i tried it with normal blocks
a=12 { a=13; even this doesn't compile }does that mean, blocks don't have their own scope...
最满意答案
一个short文字可以从Java中的int文字自动构建。 这是的情况
short e=50;但是调用一个函数并传递参数是不同的。 由于重载的可能性,函数调用应该与函数原型完全匹配。 想象一个重载函数,一个版本的int参数和另一个版本的short参数。
至于第二个问题,在大多数语言中,通常不能通过内部作用域中的相同标识符隐藏在外部作用域中声明的标识符。 我头脑中有两个例外:
1-全局变量可以被局部变量隐藏(例如在C ++中,因为Java没有全局变量)。
2-类数据成员可以被局部变量隐藏。
在这两种情况下,该语言都提供了一个解析运算符(:: for 1-和this for 2-)。
A short literal can be automatically constructed from an int literal in Java. This is the case of
short e=50;But calling a function and passing parameters is different. Because of the possibility of overloading, the function call should exactly match the function prototype. Imagine an overloaded function with an int parameter for one version and a short parameter for another.
As for the second question, in most languages, you usually cannot hide an identifier declared in an outer scope by the same identifier in an inner scope. I have two exceptions in my head:
1- A global variable can be hidden by a local variable (in C++ for example, as Java does not have global variables).
2- A class data member can be hidden by a local variable.
In both cases, the language offers a resolution operator (:: for 1- and this for 2- ).
发布评论