9

I have a small java file given below.

    class abc{
    public static void main(String args[]){
        Object a= 9;
        int b= (int)a;
        System.out.print(b);
    }
}

It gives error while compiling in cmd but not in Netbeans. Also, when I replace '(int)a' with '(Integer)a', it compiles and runs fine on both cmd and Netbeans.

    class abc{
    public static void main(String args[]){
        Object a= 9;
        int b= (Integer)a;
        System.out.print(b);
    }
}

What is the reason for this and how can I fix this?

EDIT: The error that shows up while compiling the first code is:

    C:\Users\ANKIT.ANKITSHUBHAM-PC>javac abc.java
    abc.java:4: inconvertible types
    found   : java.lang.Object
    required: int
                            int b= (int)a;
                                        ^
    1 error

EDIT: This question is not about casting. It is about why cmd and Netbeans behave differently when I cast object into int using '(int)' but behave in a same way when cast using'(Integer)'.

5
  • Can you please post the error. I think it is because NetBeans acts smart and corrects your mistake directly. Commented Feb 5, 2016 at 9:02
  • 1
    Possible duplicate of How to cast an Object to an int in java?. The former code is only valid in Java 7+; I'd guess that your netbeans is using an older compiler. Commented Feb 5, 2016 at 9:22
  • @ctst I have posted the error. Commented Feb 5, 2016 at 9:23
  • @AndyTurner No, I don't think so. I know how to cast Object into int. It is a question about why cmd and Netbeans behave differently. Commented Feb 5, 2016 at 9:26
  • My educated guess would be, that either NetBeans directly tells the compiler to interpret your code as int b= (Integer)a; or maybe even int b= (int) (Integer)a; or uses their own compiler, which knows how to handle this (as if you would cast (Integer) and unbox it directly). But that's just a shot in the dark. EDIT: Well I should have read Jiri's answer first. Commented Feb 5, 2016 at 9:30

5 Answers 5

9

What happens here:

Object a= 9;

is:

  • int with value 9 is created
  • it is wrapped in an Integer using auto-boxing
  • it is stored in a variable of type Object

Now, on the next line, Object cannot be cast to int in Java 6, because it is in fact an Integer, and not a primitive type. It can be cast to to Integer however, and then auto-unboxing takes care of extracting an int from this Integer.


Now to the "Why does it work in Netbeans then?"

Netbeans uses a different compiler (see here) than command line javac does. It probably behaves in a different way than javac and is more tolerant - perhaps it auto-unboxes the Integer when it encounters an attempt to cast it to int.

As per another answer, Java 7 supports auto-unboxing in this circumstance, so the probable reason is that your commandline javac is from Java 6 while your Netbeans uses Java 7 compiler (or higher).

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

5 Comments

How can I use the compiler used by the Netbeans in cmd?
I don't know if and ho you could do that, but I don't think you should do that - you're writing a Java program after all, so it should be compilable by standard Java compiler.
But casting using (int) is also standard as I somewhere read that Java 7 allows this format too apart from '(Integer)'.
Since, Java 7 has been around for a while, it would be safe to say your code is standard Java. The language is constantly evolving. You'd have the same issue trying to compile new ArrayList<>() on a Java 6 compiler.
@AnkitShubham see Vlad's answer, I stand corrected as for the reason for Netbeans being able to compile it. What you want is to use Java 7 (or higher) compiler on commandline, not Netbeans one though.
5

I'd say it's due to different compiler versions (or source compliance levels):

$ javac abc.java -source 1.6
warning: [options] bootstrap class path not set in conjunction with -source 1.6
abc.java:4: error: incompatible types: Object cannot be converted to int
        int b= (int)a;
                    ^
1 error
1 warning
$ javac abc.java -source 1.8
$ java abc
9

It seems like this was a change made in Java 7. See this question and associated answers.

Looking at some of the other answers, I think it would be important to point out that your code is perfectly valid Java 7 code.

You won't need the NetBeans compiler, I'd say just install Java 8 from the Oracle website and you're done. You only need to worry if your code needs to run on Java 6, case in which your code will need to be backwards-compatible.

Comments

2

You cannot cast Object to primitive type of data, maybe NetBeans do a box for you. What java version are you using to compile in the both environment?

Comments

2

The reason for this indifferent behavior was that Netbeans was using Java 7 but cmd was still using Java 6. The casting using '(int)' is not allowed in Java 6 but is allowed in Java 7.

How to use Java 7 from cmd? Answer:

  1. Open 'My Computer'

  2. Click 'System Properties' tab on the top.

  3. Click 'Advanced System Settings' on the left pane.

  4. Click 'Environment variables...' button.

  5. There will be two sections; we are bothered with that which bears the title 'System Variables'

  6. Select 'PATH' and click Edit button.

  7. Add the address of the javac of the java 7. In my case, it was "C:\Program Files\Java\jdk1.7.0_79\bin". It contained javac.exe

  8. Click OK.

    Now try running from cmd. Hope it works!

Comments

0

That's because you cannot cast Object to primitive type of data. In second code sample you are in fact casting Object to Integer and then unwrapping it to primitive int.

Comments

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.