Java experience: Very enlightening (i.)

Source: Internet
Author: User

Want to learn Java also has two years, never dare to say how proficient, but also want to talk about their feelings, to your colleagues, to help you in the technology of the road less detours.

1. About dynamic loading mechanism

Learning Java is easier than C + + to understand the idea of OOP, after all, C + + also mixes a lot of process-oriented components. Many people can recite the characteristics of the Java language, the so-called dynamic loading mechanism and so on. Of course, the concept is often first remember and then digest, but how many people really feel the dynamic loading mechanism, trying to find the details of it? To provide you with one way:

When running a Java program in a command-line window, add this useful parameter:

Java–verbose *.class

This will clearly print out the loaded class files, most of which are required by the JDK itself, and the last few lines clearly see the order in which the several classes of files that they use are loaded. Even if you declare a class object and do not instantiate it, it will not load until you actually use the instance of that class as the object. Is it possible for everyone to understand a little bit of dynamic loading? ^_^

2. The principle of finding a class file

It is recommended that you compile and run in the command-line window when you get started, and do not use ides such as JCreator or eclipse to help with those things. Try to do it yourself:

Javac-classpath Yourpath *.java

Java-classpath Yourpath *.class

Perhaps a lot of people can understand that the purpose of setting up classpath is to tell the compiler where to look for your class file. But at least the author today to understand the JVM to query the principle of class, compiler load class to rely on ClassLoader, and ClassLoader has 3 levels, from high to Low are bootclassloader (may not be accurate name), Extclassloader, Appclassloader.

These 3 loaders correspond to the compiler to find the class file priority and different paths: Bootclassloader corresponds to the jre/classes path, the compiler is the first place to find class

Extclassloader corresponds to the Jre/lib/ext path, which is where the compiler takes the first class.

The appclassloader corresponds to the current path, so it is where the compiler defaults to find class.

In fact, you can write a simple test of your own program, for any class, such as a,

Call New A (). GetClass (). getClassLoader (). ToString () and you can see the difference between placing the class file in a different path and executing it again. It is particularly important to note that if the print is null, it is represented at the highest level of Bootclassloader because it is written in C + + and does not have the name of the class loader that corresponds to Java.

The order of the search is an upward roundabout thought, that is, if this level is not found, you can only go to this level of the search, will not look down. But there seems to be a change in the character from Jdk1.4 to Jdk1.6, and no detailed information is found. So let's not give an example. Tell everyone to design this system is sun company once the technology core Gongli, a thoroughbred Chinese Oh! ^_^

So I hope you don't confuse why the total error can not find the class file, whether it is written by themselves or imported third-party jar files (Java EE often need to import).

3. About JDK and JRE

It is certain that when installing the JDK there will be a choice of whether or not to install a separate JRE, which will generally be installed together, and I recommend that you do so. Because it will help you figure out the difference:

The Jre is the Java Runtime Environment, which is the operating environment of the Java program. Since it is running, of course, to include the JVM, which is familiar with the virtual machine, as well as all the Java library class files, are packaged in the Lib directory into a jar. You can verify it yourself. As to which file is the virtual machine on Windows? Learn MFC all know what is DLL file, then we look at jre/bin/client inside there is not a jvm.dll it? That's the virtual machine.

The JDK is Java Development Kit, a Java SDK that contains a variety of class libraries and tools. And, of course, another JRE. So why do we include another JRE? Also, Jdk/jre/bin has both client and server two folders that contain a jvm.dll. Description is available for two virtual machines. Do we not know if we have noticed this point?

We all know that the JDK under the bin has a variety of Java programs need to use the command, and the JRE Bin directory is the most obvious difference is the JDK under the Javac, this is very well understood, because the JRE is only a running environment. It is not related to development, and because of this, the JDK with the development functionality will have both a client-nature JVM and a server-nature JVM under its own JRE, and only the jvm.dll of the client-only environment under the runtime will suffice.

Remember to set the Jdk/bin path in the environment variable path? This should be the first step for everyone to learn Java, the teacher will tell you not to set the words Javac and Java is not used. It is true that all commands are contained under the Jdk/bin directory. But has anyone thought that the Java commands we use are not in the Jdk/bin directory but in the Jre/bin directory? Do not believe you can do an experiment, you can put the Jdk/bin directory Java.exe cut to another place to run the Java program, found what? All ok!

So someone's going to ask? I did not set the Jre/bin directory into the environment variable ah?

Imagine if Java in order to provide for the majority of users, they do not need to develop the JDK, only the JRE can let the Java program run up, then each customer also need to manually set the environment variable how much trouble ah? So when installing the JRE, the installer automatically helps you to add the Java.exe of the JRE to the system variables, the verification method is very simple, we see the system environment variable path at the front of the "%systemroot%\system32;%systemroot%;" Such a configuration, then go to windows/system32 below to see it, found what? There is a java.exe.

If you force the ability to move Jdk/bin to the system32 variable, you can certainly force the use of Java in Jdk/jre, but I don't recommend it unless it's necessary. Running a Java program with a separate JRE is also a test in a customer environment.

So you should know more about the connections and differences between the JDK and the JRE?

PS: In fact, there are many feelings can be summed up, we should be more practical to do some research and share the experience, big direction and too cutting-edge technical discussion is necessary, but the best not too much, after all, their foundation has not been hit, what is said the latest version is actually a major obstacle to progress!

Java experience: Very enlightening (i.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.