If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

import java.io.*;

Hi Vincent,

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

Code:

import java.io.File; //which would import only the File class

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.

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.

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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

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:

java.util.Date
java.sql.Date

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,

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.