0
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"

5
  • What are the java compiler versions used on both computers? Commented Apr 13, 2016 at 10:05
  • 1
    What's the difference between the two machines? Different version of Java, perhaps? Make sure you upgrade your java on your home machine. Commented Apr 13, 2016 at 10:07
  • Btw this should compile fine and that is the right behavior. So whatever compiler you are using at your home is wrong. It is possible that it is a pre java 5 compiler Commented Apr 13, 2016 at 10:08
  • Change int[] ar to Integer[] ar. It will solve your issue. Commented Apr 13, 2016 at 10:09
  • The compiler knows that ar elements should be objects and you supply it an int - it just doesn't know how to auto-box (either the JDK itself is too old or you set the source level to < 1.5) and thus it complains. Change ar to Integer[] or use ar[0] = Integer.valueOf(12); to make it work. Commented Apr 13, 2016 at 10:10

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great help. Thanks a LOT. I just changed the java compiler compliance level from 1.4 to 1.6 and its working fine now. Thanks again
Great as it works for you you should accept the aswer to let further readers know that this is the solution...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.