Ameya
Ameya

Reputation: 11

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; I am getting the error: "Type mismatch: cannot convert from int to Object"

Upvotes: 0

Views: 2163

Answers (1)

Guillaume Barré
Guillaume Barré

Reputation: 4228

To convert 12 into an object you need al least Java 1.5, this is called Autoboxing

Autoboxing and unboxing was introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class)

So be sure to have on both machines a Java version >= 1.5

Upvotes: 2

Related Questions