A Beginner Question

Q

Qingnan Zhou

Hello, I am very new to Java.
In C++, I can use #include to include other .cpp files.
In Java, I know I am suppose to use import key word, but what type of
file should follow import? .class or .java or neither?

Thanks.

James
 
P

Paul Lutus

Qingnan said:
Hello, I am very new to Java.
In C++, I can use #include to include other .cpp files.
In Java, I know I am suppose to use import key word, but what type of
file should follow import? .class or .java or neither?

Why not open your Java programming textbook and look at the examples?
Failing that, why not look at a Java source file written by someone else?

I ask you this because it is not files that are imported using the "import"
keyword, but packages and classes.
 
R

Rowland

Qingnan Zhou said:
Hello, I am very new to Java.
In C++, I can use #include to include other .cpp files.
In Java, I know I am suppose to use import key word, but what type of
file should follow import? .class or .java or neither?

Thanks.

James

In Java, you don't import files as such, you import packages and classes.
So, if I want to import the Vector class to hold a lsit in, I would use

import java.util.Vector;

Here, java.util is the package, and Vector is the class.

However, java.util has LOTS of very useful classes -
http://java.sun.com/j2se/1.4.2/docs/api/java/util/package-frame.html - and I
might want to import them all, so I use

import java.util.*;

Now I can create a Vector:

Vector v = new Vector();

Sometimes, I don't want to import the class, as I only use it once, or the
name conflicts with another class. Then, I might just use a fully qualified
class name, without any code...

java.util.Vector v = new java.util.Vector(); // This will not require an
import statement.

When you compile your program, you need to make sure you have told the
compiler where to find the classes/packages you have imported by using
the -classpath switch, or setting the CLASSPATH environment variable.

If you do a google search, or go to java.sun.com, there is FAR more
information out there on all these concepts - I suggest you read it
through - classes and packages are very confusing to begin with, but really
useful when you use them properly.
 
J

Joona I Palaste

Qingnan Zhou said:
Hello, I am very new to Java.
In C++, I can use #include to include other .cpp files.
In Java, I know I am suppose to use import key word, but what type of
file should follow import? .class or .java or neither?

These are very different concepts. In C or C++, the #include directive
fetches the physical source code text from another file (which may have
any extension you like, .h, .c, .cpp, .c++, or whatever) and inserts it
into your source code as if you had typed it there yourself.
In Java, the import directive doesn't fetch any text from any file
anywhere. What it does is import a *NAME* - this is similar to C++'s
namespaces. For example, "import com.foocorp.util.Blah;" means "from
now on, any unqualified class name Blah means com.foocorp.util.Blah".
"import com.foocorp.util.*;" means "from now on, any unqualified names
that aren't found elsewhere are searched in com.foocorp.util". I don't
know my C++ that well, but I think "using namespace" does a similar
thing.
The actual Java source code remains perfectly safe in the other .java
files, the compiler doesn't do anything special about them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top