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

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
  • Can you please post the error. I think it is because NetBeans acts smart and corrects your mistake directly. – ctst Feb 05 '16 at 09:02
  • 1
    Possible duplicate of [How to cast an Object to an int in java?](http://stackoverflow.com/questions/3661413/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. – Andy Turner Feb 05 '16 at 09:22
  • @ctst I have posted the error. – Ankit Shubham Feb 05 '16 at 09: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. – Ankit Shubham Feb 05 '16 at 09: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. – ctst Feb 05 '16 at 09:30

5 Answers5

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

Community
  • 1
  • 1
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • How can I use the compiler used by the Netbeans in cmd? – Ankit Shubham Feb 05 '16 at 09:27
  • 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. – Jiri Tousek Feb 05 '16 at 09:34
  • But casting using (int) is also standard as I somewhere read that Java 7 allows this format too apart from '(Integer)'. – Ankit Shubham Feb 05 '16 at 09:39
  • 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. – Vlad Feb 05 '16 at 09:43
  • @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. – Jiri Tousek Feb 05 '16 at 10:02
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.

Community
  • 1
  • 1
Vlad
  • 18,195
  • 4
  • 41
  • 71
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?

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!

Community
  • 1
  • 1
Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
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.

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36