3

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 )

Community
  • 1
  • 1
ramb0
  • 127
  • 1
  • 11

3 Answers3

2

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.

2

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.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Yes, I agree. Additionally, to be picky, you (@Maisam) received "notes" (consider them `warnings`, not `errors`). Of course, the underlying issue is that Java 1.5 introduced Generics, where your collections should be "typed", and you used "untyped" (hence `unsafe`) 1.4 variants. ... adding +1 to question for showing some heart, using google, and expressing your question clearly) – BrianT. Nov 18 '14 at 03:46
1

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?

Li Chao
  • 326
  • 2
  • 7
  • yeah it makes sense that its safer to define its data type but still what will be difference of compiling with IDE and Command line tools below people said that it depends on the version, i checked it again, versions are same on the both side, picture : http://1drv.ms/1xRxl1g – ramb0 Nov 18 '14 at 00:40
  • 2
    Hi Maisam, thanks for your follow-up. Actually, when I use javac HelloWorld.java, I just got the following warning Note: HelloWorld.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. But the HelloWord.class is already there which means the java file has been compiled. So you can go ahead and run HelloWord, you will see the same output as you saw in the IDE, but still, it is unsafe – Li Chao Nov 18 '14 at 00:44
  • i was waiting for u reply + doing some research, about datatypes in arraylist and everywhere people suggested that its not good to use < object > type or no datatype because in future ur code can give bugs or do a run time error, your answer cleared the dark shadowy corner in my pentium 3 32bit brain... thanks :) – ramb0 Nov 18 '14 at 00:50