Java import Entry
Introduction
Java is a widely used programming language that allows developers to build robust, scalable, and secure applications. One of the key features of Java is its ability to import external libraries or classes into your code using the import
statement. In this article, we will explore the import
statement in Java, understand its purpose, and learn how to use it effectively in your programs.
What is the import statement?
The import
statement in Java is used to bring in external classes or packages into your code. It allows you to access the functionalities provided by these classes or packages without having to write the complete package or class name every time. It simplifies the code and enhances the readability and maintainability of your program.
Syntax of import statement
The syntax of the import
statement in Java is as follows:
import package.name.ClassName;
Here, package.name
refers to the fully qualified name of the package that contains the class you want to import, and ClassName
is the name of the class you want to import.
Importing a single class
Let's say we want to import the ArrayList
class from the java.util
package into our code. We can do so using the following import statement:
import java.util.ArrayList;
After importing the class, we can directly use the ArrayList
class in our program without having to write the complete package name every time.
Here's an example that demonstrates the usage of the import
statement:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println(fruits);
}
}
In the above example, we import the ArrayList
class from the java.util
package. Then, we create an instance of the ArrayList
class and add some fruits to it. Finally, we print the contents of the ArrayList
using the System.out.println()
statement.
Importing multiple classes
In addition to importing a single class, you can also import multiple classes from the same package using a single import
statement. To do so, you can use the wildcard character *
to specify that you want to import all the classes from a particular package.
For example, if we want to import all the classes from the java.util
package, we can use the following import statement:
import java.util.*;
Here's an example that demonstrates the usage of wildcard import:
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println(fruits);
}
}
In the above example, we import all the classes from the java.util
package using the wildcard import statement. Then, we create an instance of the ArrayList
class and add some fruits to it. Finally, we print the contents of the ArrayList
using the System.out.println()
statement.
Importing classes from sub-packages
Java allows you to organize classes into sub-packages within a package. If you want to import a class from a sub-package, you need to specify the complete package name along with the class name.
For example, if we want to import the Scanner
class from the java.util
package, we can use the following import statement:
import java.util.Scanner;
Here's an example that demonstrates the usage of importing a class from a sub-package:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
}
}
In the above example, we import the Scanner
class from the java.util
package and use it to read input from the user. We prompt the user to enter their name, read it using the nextLine()
method of the Scanner
class, and then print a personalized greeting.
Conclusion
In this article, we explored the import
statement in Java and learned how to import external classes or packages into our code. We discussed the syntax of the import
statement, importing a single class, importing multiple classes using wildcard import, and importing classes from sub-packages. Importing classes in Java simplifies the code, improves readability, and allows us to leverage the functionalities provided by external libraries. Understanding and effectively using the import
statement is essential for Java developers to build robust and efficient applications.
Gantt Chart
gantt
dateFormat YYYY-MM-DD
title Import Statement in Java
section Single Class Import
Importing Class : 2022-07-01, 1d
section Multiple Class Import
Importing Classes : 2022-07-02, 1d
section Sub-Package Import
Importing Sub-Package: 2022-07-03, 1d