Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while compiling in cmd but not in Netbeans

Tags:

java

netbeans

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)'.

like image 365
Ankit Shubham Avatar asked Feb 05 '16 08:02

Ankit Shubham


2 Answers

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

like image 129
Jiri Tousek Avatar answered Oct 05 '22 04:10

Jiri Tousek


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.

like image 28
Vlad Avatar answered Oct 05 '22 02:10

Vlad