Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java's package package

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Java's package package". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to use Java's package package" can help you solve the problem.

What is a bag?

Package is a way of organizing classes.

The main purpose of using packages is to ensure the uniqueness of the class.

For example, you write a Test class in your code. Then your colleague may also write a Test class. If there are two classes with the same name, they will conflict and the code will not be compiled through

1. Import classes in a package

Many off-the-shelf classes have been provided in Java for us to use.

①: for example, print array:

Public class TestDemo {public static void main (String [] args) {int [] array = {1 array 2, 3, 4, 5}; System.out.println (Arrays.toString (array));}}

To use Arrays, you need to import the package, see the figure:

If the top line of code is not written, it will report an error. Look at the figure:

So how to import the above package? when we write the Arrays code, IDEA will automatically pop out the option for you to choose, choose the first item and enter directly, it will help you import the package. Look at the picture:

②: another example:

The Date class defines the date and is written by the Java class library.

Public class TestDemo {public static void main (String [] args) {java.util.Date date = new java.util.Date (); / / handwritten / / get a millisecond timestamp System.out.println (date.getTime ());} when we don't guide the package.

You can use java.util.Date to introduce the Date class in the package java.util.

But this kind of writing is more troublesome. At this time, you can use the above writing method, and you can use the import statement to import the package.

Import java.util.Date;public class TestDemo {public static void main (String [] args) {Date date = new Date (); / / get a millisecond timestamp System.out.println (date.getTime ());}}

Note:

You can import a concrete class, not a specific package.

Import util package and report an error

Import concrete classes

③: another example:

If you need to use other classes in java.util, you can use import java.util.*

Import java.util.*;public class TestDemo {public static void main (String [] args) {Date date = new Date (); / / get a millisecond timestamp System.out.println (date.getTime ());}}

Do not report an error, this * can be understood as a wildcard, which represents importing all the classes under the package

Question: there are many classes under util, can you import them all at once? No, when Java deals with it, he will do it only if he needs it.

④: but we prefer to explicitly specify the class name to import. Otherwise, it is still prone to conflict.

For example:

There is a class like Date in import java.util.*;import java.sql.*;public class TestDemo {public static void main (String [] args) {/ / util and sql, so there will be ambiguity and compilation error Date date = new Date (); System.out.println (date.getTime ()) }} / / compilation error Error: (5,9) java: reference to Date is not clear. The class java.sql.Date in java.sql matches the class java.util.Date in java.util.

In this case, you need to use the full class name

Note:

Import and C++ 's # include are very different. C++ must # include to introduce other file content, but Java does not need it. Import is just to make it easier to write code. Import is more similar to C++ 's namespace and using.

Knowledge point

The difference between import and package

Package: "package", which refers to the package in which the class resides

Import: "introduce", which means to introduce the classes needed in the class.

If we want to use some code from the Java class library, we all need to import it through import.

two。 Static import

Use import static to import static methods and fields in a package.

For example, ①:

Import static java.lang.System.*;public class Test {public static void main (String [] args) {out.println ("hello");}}

So System.out.println ("hello"); can be written as out.println ("hello")

② gives another example:

Import static java.lang.Math.*;public class TestDemo {public static void main (String [] args) {double x = 30; double y = 40; / / static import is more convenient to write. / double result = Math.sqrt (Math.pow (x, 2) + Math.pow (y, 2)); double result = sqrt (pow (x, 2) + pow (y, 2)); System.out.println (result);}}

Then write the code in Math. You can get rid of it.

3. Put the class in the package

Basic rules:

Add a package statement at the top of the file to specify which package the code is in.

The package name needs to be specified as a unique name as much as possible, usually in the reverse form of the company's domain name (such as com.xuexiao.demo1).

The package name should match the code path. For example, if you create a com.xuexiao.demo1 package, there will be a corresponding path com/xuexiao/demo1 to store the code.

If a class does not have a package statement, the class is placed in a default package.

Procedure:

1) create a new package in IDEA: right-click src-> New-> package

2) enter the package name in the pop-up dialog box, such as com.xuexiao.demo1, and click enter

3) create a class in the package, right-click package name-> New-> Class, and then enter the class name.

4) at this point, you can see that the directory structure on our disk has been created automatically by IDEA.

5) at the same time, we also see that a package statement appears at the top of the newly created Test.java file.

4. Access control of packages

We have learned about public and private. Members in private can only be used internally by the class.

If a member does not contain the public and private keywords, the member can be used in other classes inside the package, but not in classes outside the package.

For example:

The following code gives an example. Demo1 and Demo2 are in the same package, Test is in other packages.

Demo1.java

Package com.bili.demo;public class Demo1 {int value = 0;}

Demo2.java

Package com.bili.demo; public class Demo2 {public static void Main (String [] args) {Demo1 demo = new Demo1 (); System.out.println (demo.value);}}

/ / execute the result, and the value variable can be accessed

ten

Test.java

Import com.bili.demo.Demo1; public class Test {public static void main (String [] args) {Demo1 demo = new Demo1 (); System.out.println (demo.value);}} / compilation error Error: (6,32) java: value is not public in com.bili.demo.Demo1; it cannot be accessed from an external package. Common system packages

1. Java.lang: the basic classes commonly used in the system (String, Object). This package is automatically imported from JDK1.1.

2. Java.lang.reflect:java reflection programming package

3. Java.net: network programming development package.

4. Java.sql: a support package for database development.

5. Java.util: is a tool package provided by java. (collection, etc.) is very important

6. Java.io:I/O programming development kit.

This is the end of the content about "how to use Java's package package". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 298

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report