简体   繁体   English

在Java中的对象数组内初始化整数时,出现“类型不匹配:无法从int转换为Object”错误

[英]Getting “Type mismatch: cannot convert from int to Object” error at the time of initiation of integer inside Object Array in java

public class objArrayPractice {
    public static void main(String[] args) {
        Object ar []= new Object [4];
        ar[0]= 12;
    }
}

when I write ar[0]= 12; 当我写ar [0] = 12时; I am getting the error: "Type mismatch: cannot convert from int to Object" 我收到错误消息:“类型不匹配:无法从int转换为Object”

To convert 12 into an object you need al least Java 1.5, this is called Autoboxing 要将12转换为对象,您至少需要Java 1.5,这称为自动装箱

Autoboxing and unboxing was introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class) Java 1.5中引入了自动装箱和拆箱功能,可将原始类型自动转换为装箱的原始类型(Object或Wrapper类)

So be sure to have on both machines a Java version >= 1.5 因此,请确保两台计算机上的Java版本均大于等于1.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Stream-编译时错误-类型不匹配:无法从Map转换 <Object,Object> 到地图 <Integer,List<String> &gt; - Java Stream - Compile time Error - Type mismatch: cannot convert from Map<Object,Object> to Map<Integer,List<String>> 错误:类型不匹配:无法使用Java Xtend从(Object)=&gt; int转换为int - Error: Type mismatch: cannot convert from (Object)=>int to int using Java Xtend 类型不匹配:无法从 Object Array 转换为 int - Type mismatch: cannot convert from Object Array to int 类型不匹配:无法从整数转换为整数 - Type mismatch: cannot convert from Integer to int 类型不匹配:无法从元素类型Object转换为int - Type mismatch: cannot convert from element type Object to int 类型不匹配:Java 代码中无法从 int 转换为 int[] 错误 - Type mismatch: cannot convert from int to int[] error in Java code Java类型不匹配-无法从构造函数中的int [] []错误转换 - Java Type mismatch - cannot convert from int[][] error in constructor Java - 类型不匹配:无法从元素类型 Object 转换为 String - Java - Type mismatch: cannot convert from element type Object to String Java类型不匹配:无法从java.lang.Object转换 - Java Type mismatch: cannot convert from java.lang.Object 类型不匹配:无法从元素类型对象转换为字符串错误 - Type mismatch: cannot convert from element type Object to String error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM