Java import

Java import: Java permits to import all the classes of a package or a single class also. This facility does not exist with C/C++. In C/C++, it is required to include all the functions of header file, whether you use only one function or all functions. Including all the functions for the use of one function is A RAM wastage; client requires more RAM to execute our software product.

Java is the latest language. Java allows even to import one class also without importing all.

Let us dig more into the subject.

import java.util.*;

In the above statement java.util is a package and * indicates to import all the classes and interfaces of the package. Suppose you are using only one class of the package instead of all. Importing all is a RAM wastage on client machine. Any software product you develop must execute on less RAM so that the product can be bought by many running on less Ram. For this no answer from C/C++. With Java import statement you can import even a single class also as follows.

import java.util.Date;

Here, * is removed. Instead of Date is written. The above statement imports only one class Date from java.util package.

Following is the example on Java import importing only Date class.
import java.util.Date;
public class Demo
{
  public static void main(String args[])
  {
    Date d1 = new Date();
    System.out.println("Today is " + d1);
  }
}

Java importOutput Screenshot on Java import

Some more examples.
import java.lang.String;                       // not import java.lang.*;
import java.lang.Math;                         // not import java.lang.*;
import java.util.Scanner;                      // not import java.util.*;

What is a package? and how to import a single class also is discussed more elaborately with different combinations in Java Import Classes.

Leave a Comment

Your email address will not be published.