错误 – 不兼容的类型:从 int 到 short 的转换可能会丢失信息。

huangapple go评论74阅读模式
英文:

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 Ntest 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.

huangapple
  • 本文由 发表于 2020年8月20日 03:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63493515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定