Difference between command line tool compiling and IDE compiling Java

1.6k views Asked by At

im new to java, ROOKIE... i was doing arraylist example and i compiled it on IDE it worked perfectly, i did that example on CMD it gave me an error

Note: Practice.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

so i used my googling power and googled everything, i found a answer,

by the way this is the code im talking about...

     import java.util.*;
     public class Practice{
     public static void main(String[] args){
     ArrayList mylist = new ArrayList();
     mylist.add("Maisam Bokhari");
     mylist.add("Fawwad Ahmed");
     mylist.add("Ali Asim");
     mylist.add("Maheen Hanif");
     mylist.add("Rimsha Imtiaz");
     mylist.add("Mugheer Mughal");
     mylist.add("Maaz Hussain");
     mylist.add("Asad Shahzada");
     mylist.add("Junaid Khan");
     System.out.println("Name of the student: "+mylist);
    }
}

it work perfectly on IDE (netbeans) but it gave those 2 errors on cmd

many people all over the internet and stackoverflow said that define the data type when creating an ArrayList object

ArrayList< String > mylist = new ArrayList<>();

i did this and it worked perfectly on CMD too... :)

now my question is this which i cant find on the internet

what is the difference between IDE compiling and command line tools compiling?

( i remember when i used to compile my C code in turboC and when i shifted to code::blocks i had to change some code to adjust the compiler , is this same thing ? but java was platform independent )

3

There are 3 answers

3
Li Chao On BEST ANSWER

this is an interesting question, I have tried your code in the Eclipse IDE, in which it is not fine at all because Eclipse will report a warning which says:

Type safety: The method add(Object) belongs to the raw type List. References to generic type List<E> should be parameterized

This is why your following operation is not safe. Please see this example, if you do not specify the data type of the list, you can add any type of data in the list

 List a = new ArrayList();
 a.add("abc"); //add a string object
 a.add('a'); //add a char
 a.add(1); // add a integer

So when you operate the element in the list, it will be unsafe. Does this make sense to you?

0
Ross H Mills III On

One reason for the difference is that your IDE might be using a different version of java than your command line compiler. You would see this if your Netbeans IDE uses java 1.4 or older and your command line uses 1.5 or newer.

1
Makoto On

Your IDE would report the same thing, depending on the compiler version. To my recollection, Java 1.4 would not report that as an issue, but 1.5 and newer versions would, due to the fact that you're going against the grain of generics.

If done correctly, there is no difference between compilation done on the command line and compilation done by the IDE. But this means you have to be sure that the same version of Java is used to compile the code across the two.