-
Package(directory)
Java classes can be grouped together in packages. A package name is the same as the directory (folder) name which contains the .java files.
-
Package declaration
The first statement, other than comments, in a Java source file, must be the package declaration.
-
Default package
Although all Java classes are in a directory, it's possible to omit the package declaration. For small programs it's common to omit it, in which case Java creates what it calls a default package.
-
Package declaration syntax
The statement order is as follows. Comments can go anywhere.
- Package statement (optional).
- Imports (optional).
- Class or interface definitions.
-
Imports: three options
- The wildcard character (*) is used to specify that all classes with that package are available to your program.
i.g:
import javax.swing.*; // Make all classes visible altho only one is used.- Classes can be specified explicitly on import instead of using the wildcard character.
i.g:
import javax.swing.JOptionPane; // Make a single class visible.- Alternately we can the fully qualified class name without an import.
i.g:
javax.swing.JOptionPane.showMessageDialog(null, "Hi"); -
Common imports
There are 166 packages containing 3279 classes and interfaces in Java 5. However, only a few packages are used in most programming. GUI programs typically use at least the first three imports.
import java.awt.; Common GUI elements. import java.awt.event.; The most common GUI event listeners. import javax.swing.; More common GUI elements. Note "javax". import java.util.; Data structures (Collections), time, Scanner, etc classes. import java.io.; Input-output classes. import java.text.; Some formatting classes. import java.util.regex.*; Regular expression classes.
-
import FAQ
-
Q: Does importing all classes in a package make my object file (.class or .jar) larger? A: No, import only tells the compiler where to look for symbols.
-
Q: Is it less efficient to import all classes than only the classes I need? A: No. The search for names is very efficient so there is no effective difference.
-
Q: Doesn't it provide better documentation to import each class explicitly? A: This shows good intentions, but ...
- It's hard to remember to remove classes when they are no longer used, so the import list is surprisingly often wrong. It can seriously slow down reading because unusual or unexpected class imports make me look for that class, only to discover that it must have been used in an earlier version.
- Explicit class imports permit accidentally defining classes with names that conflict with the standard library names. This is very bad. Using "*" to import all classes prevents this dangerous naming accident.
- It's annoying to always update this list, altho if you use NetBeans, fixing the list is only a click away (see below). Q: I've imported java.awt., why do I also need java.awt.event.? A: The wildcard "*" only makes the classes in this package visible, not any of the subpackages.
-
Q: Why don't I need an import to use String, System, etc? A: All classes in the java.lang package are visible without an import.
-
Q: Is the order of the imports important? A: No. Group them for readability. A: No. Group them for readability.
-
- For Loop
A for loop is a control structure that allows us to repeat certain operations by incrementing and evaluating a loop counter.
i.g:
for(initialization;Boolean-expression;step)
statement;
for(int i=0;i< 5;i++){
System.out.println("Simple for loop: i = "+i);
}- While Loop
The while loop is Java's most fundamental loop statement. It repeats a statement or a block of statements while its controlling Boolean-expression is true.
i.g:
while(Boolean-expression)
statement;
int i=0;
while(i< 5){
System.out.println("While loop: i = "+i++);
}- ** Do-While Loop**
The do-while loop works just like the while loop except for the fact that the first condition evaluation happens after the first iteration of the loop.
i.g:
do {
statement;
} while (Boolean-expression);
int i = 0;
do {
System.out.println("Do-While loop: i = " + i++);
} while (i < 5);