what does the the "import" statement do for the program on earth?

F

Friday

I am a beginner of Java.
I don't understand what the the "import" statement do for the
program clearly.
For example, I just write a program like this:

import java.util.HashMap
class MyClass
{ ... }
...


In my opinion, the computer first copy all the code of the
"java.util.HashMap" class to
my program,then go down to the next .
Isn't it?
Thanks for your attention.
 
A

Andrew Thompson

Friday said:
I am a beginner of Java.

With usenet as well, it seems.

Please refrain from multi-posting, in future.

(X-post to c.l.j.p/h., w/ f-u to c.l.j.h. only)

Andrew T.
 
F

Furious George

Friday said:
I am a beginner of Java.
I don't understand what the the "import" statement do for the
program clearly.
For example, I just write a program like this:

import java.util.HashMap
class MyClass
{ ... }
...

A simple example should be sufficient to explain

/*use import*/
import java.util.HashMap; public class MyClassWithImport { private
HashMap map /*no need for fully qualified name*/; }

/* no import */
public class MyClassWithoutImport { private java.util.HashMap /*must
use fully qualified name*/ ;}

/*Something to look out for*/
import java.util.List ;
public class MyClassWithDuplicates {
private List someList;
private java.awt.List otherList /*must use fully qualified name or
compiler will make mistake*/; }

In my opinion, the computer first copy all the code of the
"java.util.HashMap" class to
my program,then go down to the next .
Isn't it?

No. All import does is save keystrokes.
 
D

Daniel Pitts

Friday said:
I am a beginner of Java.
I don't understand what the the "import" statement do for the
program clearly.
For example, I just write a program like this:

import java.util.HashMap
class MyClass
{ ... }
...


In my opinion, the computer first copy all the code of the
"java.util.HashMap" class to
my program,then go down to the next .
Isn't it?
Thanks for your attention.

Actually, what import does is to let the compiler know that class
exists.
import java.util.HashMap;
tells the compiler that when you say "HashMap" later in the source
code, you are refering to the java.util.HashMap class specifically.

It doesn't actually change your program at all, just the source code to
the program.
 

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,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top