简体   繁体   中英

Java: compilation error with the command prompt

import java.util.*;

public class Lab8 {
          public static void main(String str[]) {
                    List list=new ArrayList();
                    list.add("sri");
                    list.add("Nivas");
                    list.add("Dande");
                    list.add("JLC");
                    System.out.println(list);

                   Iterator it=list.iterator();
                   while(it.hasNext()) {
                               Object obj=it.next();
                               System.out.println(obj);
                               if(obj.equals("JLC")) 
                               it.remove();
                    }
                    System.out.println(list);
          }
}

Problem:

While compiling the above code with an IDE, it works just fine but when I compile it
using the command prompt, the compiler throws an error as:

 error: incompatible types
        Object obj=it.next();
                      ^
  required: Object
  found:    java.lang.Object

Why I am getting this error with the command prompt and not with the IDE?

I tried running your code on my system and I got a different error in my command prompt (windows).

Note: Lab8.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Well, maybe our system handles this kind of problems a bit differently, but the issue with your code is the lack of generics. You are adding String objects to the List, so you should specify it as such. Without being specific, you could add in String, ints, and other stuff and the subsequent operations that you perform on the objects in the List aren't always guaranteed to work (well in this case, there isn't much of a problem because you're just printing things out, but nonetheless, JVM will still warn you of this before it compiles your code).

Here you go. Notice the use of the angle brackets < > to specify the type more strictly (a good practice in a statically typed language like Java).

import java.util.ArrayList;
import java.util.Iterator;

public class Lab8 {
    public static void main(String str[]) {
        ArrayList<String> list=new ArrayList<String>();
        list.add("sri");
        list.add("Nivas");
        list.add("Dande");
        list.add("JLC");
        System.out.println(list);

        Iterator<String> it=list.iterator();
        while(it.hasNext()) {
            Object obj=it.next();
            System.out.println(obj);
            if(obj.equals("JLC")) 
                it.remove();
        }
        System.out.println(list);
    }
}

Eclipse allows your old code to be built and run, but attempting to compile in the command line shows the compilation warning. Why the difference? Well, Eclipse assumes that you have read its compilation warnings already (which are displayed in the IDE itself by the left of the code, with a tiny light bulb symbol), and hence lets you build and run the program if you so desire. There is no such feature in the command line, so they have to give you some explicit warning message.

In any case, I hope you do not have any more errors using the code I provided. If you still have errors, then maybe it is another issue.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM