In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the function of the java advanced package". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of the java advanced package".
Step 1 there are many tool classes in the toolkit
I talked about the method of printing data before: System.out.println, it's a bit troublesome to write so many words. So we might as well create a StringUtil,Util which means a tool, in which a newLine method is designed for string printing.
StringUtil is a utility class and should be placed in a toolkit called tool. This tool is a folder, and I temporarily created it in the root directory of disk D.
Step 2 StringUtil
To create a StringUtil.java, you don't have to think about coding. Now it must be ANSI, which is actually GBK, unless the system you are using is not simplified Chinese. Anyway, it supports Chinese. Open it with Editplus and type in the following code:
Class StringUtil {public static void newLine (String msg) {System.out.println (msg);}}
Compile it with javac to get the .class file.
Step 3 current situation
The directory structure is as follows:
Our program is in the java18 folder, and now we want to call the newLine method in the StringUtil in the tool folder.
Modify Demo.java
Class Demo {public static void main (String [] args) {StringUtil.newLine ("StringUtil-> newLine");}}
Compile the code:
No wonder JVM knew there was a StringUtil class when it was said that the symbol could not be found.
Step 4 CLASSPATH
If you have studied java and have some basic knowledge of Java, then you must know the name of CLASSPATH. The so-called CLASSPATH is the words class and path, which means "classpath". It is you who tell JVM which folders are the paths where I store classes (class files).
This thing, which also needs to configure environment variables, remember the steps to configure JDK environment variables?
Now let's configure the CLASSPATH environment variable, and if you don't remember, you can go back to the previous chapter.
Step 5 specific configuration
We are usually logged in by admin users, so we directly create a new system variable, we configure the D disk root directory, and there is a period.
Dorex.
The D:\ on the left represents the D root directory, and the period on the right represents the current directory. For example, if you want to java Demo in the java18 folder, JVM needs to know that the current directory is the java18 folder.
CLASSPATH this thing, you either do not configure, do not configure the default is the current directory, once configured, do not forget to add a dot.
I'm afraid you don't understand. Again, this configuration means all classpath. After we have configured this, JVM will use the D disk root path as well as the current directory where you execute the program as the root path, which will be explained in more detail later.
Step 6 package
The root directory of disk D is our classpath, which has been configured. But StringUtil is not in the root directory of disk D, but there is a folder called tool in disk D. In order for JVM to know, we also have to modify StringUtil, plus package.
Package tool;class StringUtil {public static void newLine (String msg) {System.out.println (msg);} step 7 verification
The tool folder is also called a package. Now let's do the verification and add a main method to the StringUtil.
Package tool;class StringUtil {public static void newLine (String msg) {System.out.println (msg);} public static void main (String [] args) {newLine ("StringUtil");}}
Compile with the Ctrl+1 of EditPlus, and then open the CMD black window outside. You don't need to enter the D disk and enter the command directly.
Tool.StringUtil, this "." Very spiritual, JVM will put "." Translated into\.
Because there is D:\ in CLASSPATH, the location finally found is: d:\ tool\ StringUtil
Now, it's very clear.
Step 8 do it yourself
It is strongly recommended to do the above experiment by yourself, I believe many people do not pay attention to this principle, because IDEA really smells good. But, as a programmer, you still need to know something. There is something to brag about in the future, isn't it?
Step 9 Why is it wrong to run with Editplus now?
You can try to run StringUtil in Editplus and you will get an error.
The reason is simple, because you added package.
Package tool
If you don't add it, it will be a default default package, that is, all the paths in the CLASSPATH. Once you add this sentence, then when running, JVM will have to take it to a higher plane of principle, first look in CLASSPATH, and then correspond to see if there is a tool folder inside. What do you mean by running directly? it means that you plan to find a StringUtil.class to run directly in CLASSPATH. However, the D disk does not have this StringUtil.class, and there is in the current directory, but there is a package tool at the beginning of others. It thinks it is under D:\ tool and refuses to run it for you, so it cannot be found.
Unless you get rid of package tool;.
Step 10 is straightforward.
To put it bluntly, the sentence just now is
Java StringUtil
Do not add any package, naked, this is called the default package (default package), the default package is where you configure CLASSPATH.
The role of step 11 package
Finally, the title ha, package has two functions, 1 is to prevent class duplicate name, method duplicate name. For example, we wrote StringUtil, but as far as I know, many third-party jar packages have StringUtil. Wouldn't it be a mess if we didn't use package to distinguish it?
2 is to better manage class files, after all, you a project has a lot of class, hundreds of thousands of possible, do not need package to classify, then how to maintain this project?
Thank you for your reading, the above is the content of "what is the role of the java advanced package". After the study of this article, I believe you have a deeper understanding of what the role of the java advanced package is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.