Home > Java > javaTutorial > body text

What are packages in java? Why use java package

青灯夜游
Release: 2018-11-26 17:17:07
Original
10311 people have browsed it

The content of this article is to introduce what are the packages in java? Why use java package. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What are the packages in java?

A package in Java is a mechanism that encapsulates a group of classes, sub-packages and interfaces and can be used to organize a group of related classes and interfaces. Conceptually, we can think of packages as similar to different folders on your computer: you can keep HTML pages in one folder, images in another, scripts, or scripts or applications. in another folder.

Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to stay organized by putting related classes and interfaces into packages.

There are two types of packages in Java: built-in packages and packages we create ourselves (also called user-defined packages)

Benefits of using java packages

This is the reason why Java packages are used:

1. Reusability. When developing projects in Java, we often feel that there is very little written in our code over and over again. Using a package you can create something like this as a class inside the package and whenever you need to perform the same task just import the package and use the class.

2. Used to classify classes and interfaces to better organize and maintain them. In our large Java projects with hundreds of classes, there is always a need to group similar types of classes into a meaningful package name so that you can better organize the project and quickly locate it and use it when needed , which improves efficiency.

3. Prevent naming conflicts. We can define two classes with the same name in different packages to avoid name conflicts, and we can use the package

Related knowledge of Java packages:

A package is a container for a set of related classes, some of which are accessible while others are reserved for internal purposes.

How does the package work?

Package name and directory structure are closely related. For example, if the package name is college.staff.cse, then there are three directories, college, staff and cse, so that cse exists in staff and staff is in college. In addition, the directory College can be accessed through the CLASSPATH variable, that is, the parent directory path of college exists in CLASSPATH. The idea is to make sure classes are easy to find.

Naming convention for packages: Packages are named in reverse order of domain names, i.e. org.geeksforgeeks.practice. For example, in college, the recommended conventions are college.tech.cse, college.tech.ee, college.art.history, etc.

Add classes to package: We can add more classes to the created package using the package name at the top of the program and save it in the package directory. We need a new java file to define a public class, otherwise we can add the new class to the existing .java file and recompile it.

Sub-package: A package within another package is a sub-package. They are not imported by default and must be imported explicitly. Furthermore, members of subpackages have no access rights, i.e. they are treated as different packages with protected and default access specifiers.

Package types in Java

There are two types of packages in java.

1. Built-in packages: Packages that have been defined in java, such as: java.io. *, java.lang.*, etc. are called built-in packages.

2. User-defined packages: The packages we create ourselves are called user-defined packages.

Simple example of Java package:

1. Create a package in java

We can use the package keyword in Java creation package.

//保存 Simple.java  
package mypack;  
public class Simple{  
 public static void main(String args[]){  
    System.out.println("欢迎来到包装");  
   }  
}
Copy after login

2. Compile the created package

If you do not use any IDE, you need to follow the following syntax:

javac -d  目录  javafilename
Copy after login

For example:

javac -d . Simple.java
Copy after login
Copy after login
# The ##-d switch specifies the destination location to place the generated class files. You can use any directory name like /home (if linux), d:/abc (if windows) etc. If you want to save the package in the same directory, you can use "." (dot).

3. Run the java package

We need to use a fully qualified name, such as mypack.Simple, etc. to run the class.

Compile:

javac -d . Simple.java
Copy after login
Copy after login

Run:

 java mypack.Simple
Copy after login
Run output:

What are packages in java? Why use java package

Summary: The above is the summary of this article All content, I hope it will be helpful to everyone's study.

The above is the detailed content of What are packages in java? Why use java package. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template