英文:
error - : incompatible types: possible lossy conversion from int to short
问题
我知道当我们尝试将值进行向下转换时会出现此错误,但在我的代码中,我无法找出我在哪里进行了向下转换的值。
class TestClass {
public static void main(String args[]) throws Exception {
TestDemo obj = new TestDemo();
TestDemo2 obj1 = new TestDemo2();
obj.show(5);
obj1.show("helloworld");
}
}
class TestDemo {
public void show(short N) {
System.out.println(N * 2);
}
}
class TestDemo2 {
public void show(String S) {
System.out.println(S);
}
}
英文:
I know this error occurs when we try downcasting values but in my code I am not able to figure ooout where have I downcasted the values.
class TestClass {
public static void main(String args[] ) throws Exception {
TestDemo obj=new TestDemo();
TestDemo2 obj1= new TestDemo2();
obj.show(5);
obj1.show("helloworld");
}
}
class TestDemo{
public void show(short N){
System.out.println(N*2);
}
}
class TestDemo2{
public Void show(String S){
System.out.println(S);
}
}
答案1
得分: 3
这个错误是由于 obj.show(5)
引起的。
你可以进行以下两个修复之一:
第一个版本:
class TestClass {
public static void main(String args[]) throws Exception {
TestDemo obj = new TestDemo();
TestDemo2 obj1 = new TestDemo2();
obj.show((short) 5);
obj1.show("helloworld");
}
}
class TestDemo {
public void show(short i) {
System.out.println(i * 2);
}
}
class TestDemo2 {
public void show(String S) {
System.out.println(S);
}
}
第二个版本:
class TestClass {
public static void main(String args[]) throws Exception {
TestDemo obj = new TestDemo();
TestDemo2 obj1 = new TestDemo2();
obj.show(5);
obj1.show("helloworld");
}
}
class TestDemo {
public void show(int i) {
System.out.println(i * 2);
}
}
class TestDemo2 {
public void show(String S) {
System.out.println(S);
}
}
英文:
This error is occurring due to obj.show(5).
Two fixes`you can do any:
class TestClass {
public static void main(String args[] ) throws Exception {
TestDemo obj=new TestDemo();
TestDemo2 obj1= new TestDemo2();
obj.show((short)5);
obj1.show("helloworld");
}
}
class TestDemo{
public void show(short i){
System.out.println(i*2);
}
}
class TestDemo2{
public void show(String S){
System.out.println(S);
}
}
Second Version
class TestClass {
public static void main(String args[] ) throws Exception {
TestDemo obj=new TestDemo();
TestDemo2 obj1= new TestDemo2();
obj.show(5);
obj1.show("helloworld");
}
}
class TestDemo{
public void show(int i){
System.out.println(i*2);
}
}
class TestDemo2{
public void show(String S){
System.out.println(S);
}
}
答案2
得分: 1
尝试将public void show(short N)
中的short N
从test Demo
更改为int
。
英文:
Try changing the short N
, in public void show(short N)
from test Demo
to int
.
答案3
得分: 0
尝试先将整数转换。
obj.show((short)5);
英文:
try to cast the int first.
obj.show((short)5);
答案4
得分: 0
Number 5 is treated by default as an integer which is passed to a method with a short argument.
obj.show((short)5);
Also for future reference, Java errors as well as exceptions are very detailed, giving you the exact line number where the issue occurred. That should help you identify the code segment where the issue is.
英文:
Number 5 is treated by default as integer which is passed to method with short argument.
obj.show((short)5);
Also for future reference, java errors as well as exceptions are very detailed, giving you the exact line number where the issue occurred. That should help you identify the code segment where the issue is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论