December 3rd, 2002, 04:11 AM
what does "import java.io.* mean?
Here is a sample program:
\* A simple example program *\
class ASimple Example
public static void main (String args)
What does "import java.io.*" mean? What are we importing? If we had not imported java.io, what would have happened when we tried to run our program?
PLEASE SOMEONE HELP ME WITH THIS QUESTION!!!
PLEASE EMAIL TO ME AT email@example.com
December 3rd, 2002, 04:38 AM
If you're familiar with C, C++, "import ...." in java is analogous to the "<#include ...>" of C, C++.
Coming closer to your question, import java.io.*; imports all the classes that are defined in java.io package to your file. This enables your java program to use those classes and their methods to achieve some task.
An example for this might be, that if you want to work around with files on disk by reading / writing to them through a java application, you need the java.io.File class. Here, you could have stated like
but if you needed more than one file to be imported in your program (and that you're lazy enough to write a line for each such class, you could use import java.io.*; statement.
import java.io.File; //which would import only the File class
You can see what classes are there in java.io by going through the java documentation @ sun's site http://java.sun.com/j2se/1.4.1/docs/api/.
Answering your third question:
What would have happened if we had not imported java.io.*?
This question cannot be answered completely at the point as I do not know what exactly you're doing - I mean the code of the rest of the program is unknown.
I can only say that if you're using a class in a package in your program without importing it from the java.io package, you'd get an compilation error.
Hope this helps
Tata n Take care
December 3rd, 2002, 05:55 AM
The import statement tells the compiler where to look for the external classes you use in your code. It needs to find them to check that you are using them correctly, calling methods that exist, passing the right arguments to them, etc.
If you import java.io.* the compiler will look at all classes in the java.io package when it needs to find a class you are using. It will actually look for the .class files in the java\io directory (and in this directory path inside any jar files) on the classpath.
This particular import happens to be the Java Input/Output package containing the .class files for reading and writing data.
If you used one of the classes in this package without the import statement (and without explicitly specifying its package when you used it), the compiler would be unable to find it and would output an error message 'Cannot resolve symbol, class Xxxxx'. This also happens if the .class files you try to import are not actually present in the package directory or in a jar file on the classpath.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
December 3rd, 2002, 06:47 AM
Packages are a key component to Java. When you define any class, by default, Java imports a package called java.lang. This happens seamlessly, and this package includes all basic functional classes deemed standard. If your application requires the use of additional classes, you import the package(s) where they reside. The example you gave imports the io package, (and all its sub-classes*) which resides in the java directory. Packages actually work the same way folders do in a file diretory hierarchy. The java package names not only refer to the actual package, but the path of where they are located. For example, there are two Date classes:
These two java classes reside in different locations. The sql Date class extends the other Date class, so they share commonalities, but they are separate classes with the same name, and therefore must reside in separate location.
Getting back to your example, one would import the io package to add functionality to the class. This package contains classes with lots of methods pertaining to io operations such as file handling. If you want to avail yourself of these classes, they must be imported.
I guess another angle to look at is the notion that it would not make sense for any programming language to include all API's with each application developped. The developper must have the flexibility to be able to tailor their code to meet the specific needs of the app itself with the least amount of overhead. In VB for example, you wouldn't make project references to components not actually used in your app. This would add unneccessary demands on your app. Java allows you to precisely specify which classes are required through the import statement. You can have unlimited import statements. For further information on all classes, download the documentation that comes with the SDK to view the full API descriptions (this tool is indispensible!). Cheers,
Click Here to Expand Forum to Full Width