Java programming experience

Source: Internet
Author: User

A good original reprinted

Http://blog.sina.com.cn/s/blog_530abec50100i1ez.html

I have been studying Java for two years. I can never say how proficient I am, but I want to talk about my feelings and write it to my colleagues at the Software Institute, this helps you avoid detours on the technical road. If I speak louder, I hope everyone will compete for the Software Institute. In fact, the most important thing is the improvement of everyone's own progress ??

1. What about the dynamic loading mechanism ??
Learning Java is easier to understand the idea of OOP than C ++. After all, C ++ has a lot of process-oriented components. Many people can back up the features of the Java language, the so-called dynamic loading mechanism, and so on. Of course, the concept is often first remembered and then digested. How many people have actually realized the dynamic loading mechanism and tried to find details?
You can use the following methods:
When running a Java program in the command line window, add this useful parameter:

Java? Verbose*. Class

This will clearly print the loaded class files, most of which are required for JDK's own operation. The last few lines will clearly see the order in which the class files used are loaded. Even if you declare a class object, it will not be loaded without instantiation. This means that the class will be loaded only when the class instance is actually used. In this case, do you understand a little bit about dynamic loading? Pai_^

2. How does one find a class file ??
It is recommended that you compile and run in the command line window at the beginning, instead of using jcreator, eclipse, and other ide To help with those tasks. Try to do this yourself:
Javac-ClasspathYourpath*. Java
Java-ClasspathYourpath*. Class
Many people may understand that the purpose of setting classpath is to tell the compiler where to find your class file.However, at least the author understands the principle of JVM to query classes today. The compiler must rely on classloader to load classes,
Classloader has three levels, from high to low, which are bootclassloader (the name may be inaccurate),Extclassloader,
Appclassloader.

These three loaders correspond to the compiler to find the class file priority and different paths: bootclassloader corresponds to the JRE/classes path, which is the place where the compiler first looks for the class
Extclassloader corresponds to the JRE/lib/EXT path, which is the place where the compiler first looks for the class.
Appclassloader corresponds to the current path, so it is also the place where the compiler finds the class by default.

In fact, you can write a simple program for testing, for any class, such as,
Call NewA (). getclass (). getclassloader (). tostring ()Print it out and you will see the difference. If you put the class file in different paths and execute it again, you will see the difference. Note that if it is null, it indicates the highest level of bootclassloader,
Because it is written in C ++, the name of the Class Loader corresponding to Java does not exist.

The order of search is a kind of thought, that is, if this level cannot be found, it can only be found above this level, rather than looking down. However, it seems that the features from jdk1.4 to jdk1.6 have changed and no details are found. So we will not give an example. I would like to tell you that sun designed this system as a technology core Mr. Gong Li, a purebred Chinese! Pai_^

In this way, we hope that you will not be confused why class files cannot be found in the general error reports, whether written by yourself or imported third-party jar files (which often needs to be imported in J2EE ).

3. About JDK and JRE ??
You must choose whether to install a separate JRE when installing JDK. It is usually installed together. I also recommend that you do this. This helps you better understand the differences between them:

JREIs JavaRuntimeEnvironment,Is the runtime environment of Java programs. Since it is running, of course it should include JVM, that is, the virtual machine that everyone is familiar,
The class files of all Java class libraries are packaged into jar files in the lib directory. You can verify it by yourself. Which file is the virtual machine on windows?Anyone who has learned MFC knows what DLL files are. So let's see if there is a JVM. dll in JRE/bin/client? That is, virtual machines.

JDKIs JavaDevelopmentKit is a Java development kit that contains various class libraries and tools. Of course, it also includes another JRE.
So why should we include another JRE? In addition, JDK/JRE/bin contains both the client and server folders and a JVM. dll.
There are two virtual machines. I don't know if you have noticed this yet?

I believe everyone knows that JDK's bin contains the commands required by various Java programs. The most obvious difference with the bin directory of JRE is that javac is available under JDK, which is very understandable, because JRE is just a runtime environment. It has nothing to do with development. Because of this, JDK's own JRE with the development function will have a client JVM and a server JVM,
In the JRE environment, only client JVM. dll is required.

Do you remember to set the JDK/bin path in the environment variable path? This should be the first step for everyone to learn Java,The instructor will tell you that javac and Java will not be used if they are not set. The JDK/bin directory contains all the commands. But have you ever wondered whether the Java commands we use are not under the JDK/bin directory but under the JRE/bin directory? Do not believe that you can create an experiment. You can cut the java.exe file in the JDK/bindirectory to another place and run the Java program. What did you find? Everything is OK!

So someone will ask? I have not set the JRE/bin directory to the environment variable?

If Java is provided to most people, they don't need JDK for development, but they only need JRE to run the Java program, so every customer needs to manually set the environment variable. How troublesome is it? When jreinstallation is used, the installation program automatically adds jrejava.exe to the system variables. The verification method is very simple. You can see that the system environment variable path is prefixed with "% SystemRoot % \ system32; % SystemRoot %, so let's go to Windows/system32 and see what we found? There is a java.exe.

If JDK/bin can be forcibly moved to the front of the System32 variable, of course, the Java in JDK/JRE can also be forced. However, unless necessary, I do not recommend that you do this. Using a separate JRE to run a Java program is also a test in the customer environment.

Now, we should be more clear about the connection and differences between JDK and JRE?

PS:In fact, there are many feelings to sum up. I am too many to write at a time, so I am afraid that everyone will throw bricks and kill me. We should be more steadfast and more pragmatic in doing some research and sharing experiences with each other. The general direction and too-cutting technology discussions are necessary, but we 'd better not be too many. After all, we have not laid a solid foundation yet, talking about the latest version is actually a major obstacle to progress!

######################################## ######################################## ######################################## #
JavaLearning Miscellaneous (2)
In view of the feelings of the last write-back, We all encouraged our younger brother to continue writing, so it was hard to wait until the National Day Golden Week, and the internship finally had a rest phase, so we started to write the second article. I hope this writing will still be helpful to like-minded friends. Last, I talked about the Java Dynamic Loading Mechanism, classloader principle, and three questions about JDK and JRE. What are some specific class libraries ??

1.About the set framework class
I believe that anyone who has learned Java is familiar with this term.Java. util. * The package is certainly no stranger. I don't know how to review or analyze a package when querying an API. The two most important parts of each package are interfaces and classes. The interface represents what it can do, the implementation class represents how it is implemented. Before paying attention to the Implementation class, we should first understand its source interface, whether in j2se or J2EE, it should be like this. First, let's look at these three interfaces: List, set, and map.
Some people may not be familiar with these three names, but I believe most people are familiar with arraylist, rule list, treeset, hashset, hashmap,The name of the implementation class, such as hashtable. The differences between them are easy to understand. You can put a list of repeated object sets, set put a combination of non-repeated objects, and map put
<Key, Value> Such a name-value pair,Key cannot be repeated, and value can. There are several confusing issues:

What is the difference between vector, arraylist, hashtable, and hashmap?
Many interviewers like to ask this question. In fact, they should be more professional and ask: what are the differences between the new collection framework and the old collection framework? For the new collection framework, you can find since in these packages.Jdk1.2. Previous classes such as vector and hashtable are included in the old collection framework. What is the difference?
A.The naming of the new collection framework is more scientific and reasonable. For example, arraylist and rule list under List
B.All of the new collection frameworks are non-thread-safe. We recommend that you go to the source code contained in JDK and check the differences between vector and arraylist. Of course, after JDK, it would be a little ugly because the generic syntax is added, similar to the C ++ template syntax.

Have you ever wondered why you should lock all the old collection framework by default to prevent multi-thread access from being updated to cancel all the locks in the new collection framework? By default, multithreading is supported? (Of course, you can use the static method of collections to lock the thread when needed)
In my opinion, the development of any technology may not follow their original intention. Many major changes are influenced by the objective environment. Do you know why Java was originally developed? Is developed for embedded programs. Do you remember the classloader mechanism mentioned in the previous article? It is designed to save memory in the embedded development environment. Today, Java has become the language for the birth of the Internet. What does Internet mean? Multithreading is an inevitable trend. The objective environment is changing, and Java technology is also developing rapidly, which leads to an increasing departure from its original intention. It is said that sun is actually focusing on j2se, and the result is that due to objective environmental impact, j2se is almost forgotten. It is always focused on J2EE.

The technical details are not mentioned here. Only when used can we truly understand them. This is to help you understand the technologies you are learning and want to learn. We will discuss it later when talking about J2EE.
The IT giant was IBM a few decades ago, and the mainframe market was incomparable. How does Microsoft beat IBM? It is precisely because of the rapid development of hardware that the objective environment for personal PC needs makes Microsoft the second giant through OS. What about the next defeat of Microsoft? Google. How to do it? If Microsoft does not compete with IBM for a mainframe, Google uses the objective environment of rapid Internet development as a decisive factor to avoid competing with Microsoft for OS, but uses the search engine path, known as 3rd giants. So who are the 4th giants? Many experts predict that they will appear in Asia or China,
Whatever, objective environment change trend is the key to determining the general direction. Of course, I also hope it will appear in China, ^ _ ^ ~~

2.Java Design Patterns
Many of them are looking at the 23 Design Patterns of gof. It seems that they have become a popular style to learn about gof at school or in the workplace. I don't want to list the 23 DesignsPattern,
My original intention of writing this is to talk about my own experiences and opinions, hoping to help you understand them.
First of all, I think the design pattern is only a general solution to a type of problem. As long as it is object-oriented programming predictions, these 23 kinds can be used. The best way to understand them is to write each method in person, even a simple application is enough. If the code implementation cannot be remembered, remembering the corresponding UML diagram will be a good way. Of course, the premise is that you must understand UML.
At the same time, it is best to use Java's own class libraries to help with memory, such as the commonly used observer mode, in Java. util. * There is a ready-made observer interface and Observable implementation class. If you look at the source code, it is sufficient to understand the observer mode. For example, in the decorator mode, you only need to write a few Java. Io. * programs to fully understand what the decorator mode is. Many people think that they should not be in touch with the design model when getting started. The famous java Design Pattern in beiling design series is written:
StevenJohnMetsker, I am also confused by many examples. However, I still don't agree that there is any conflict between getting started and learning design patterns, but we only need to know the concepts and typical applications of each pattern, so that we can write
Fileoutputstream, bufferedreader, and printwriter can feel that the original design pattern is so close to us, and it is not so mysterious.

In addition, while learning some patterns, it helps us better understand some characteristics of Java class libraries. For example, when you compile the prototype mode, you must understand thatThe Java. Lang. cloneable interface and the clone () method of the base class object of all classes. That is, the difference between deep copy and light copy:
Object. by default, clone () implements shortest copy, that is, copying an object copy. However, if the object contains references of other objects, it does not copy references, so the original object and the copy share the referenced object.
Deep copy, of course, involves copying all references of objects. In this way, both the original object and the copy object have a reference object. To implement deep copy, you must first implementJava. Lang. cloneable interface, and then rewrite the clone () method. Because the clone () method in the object is signed by protected, and
The function of the cloneable interface is to enlarge protected to public so that clone () can be overwritten.

So there is another problem? What if the referenced object references another object? Is it very troublesome to keep judging and copying it? Some predecessors once told me that the method is to serialize the original object to the disk and then deserialize it when rewriting the clone method, so that a deep copy result can be obtained without judgment. If you do not know the serialization method, let's take a look.
Objectoutputstream and objectinputstream

In the final analysis, a pattern is just an ideological thing. It is actually nothing more than an experience summed up by our predecessors. We encourage you to write your own data, such as the proxy mode. You can simply write a Child class,Adult class. Child needs to buy anything and is implemented by adult proxy. In short, the buy () in adult actually calls the child buy (), but the main function is exposed to adult. Buy (). Such a simple program is enough to understand the basic meaning of the proxy mode.

######################################## ######################################## ################################
JavaMiscellaneous (3)
This is the third Java note I have written. I am glad that the first two articles have been supported and encouraged by Forum friends. I hope you can continue to correct the shortcomings. I have always been eager to use this method to find technical shortcomings and hope to improve it with my colleagues who are interested in Java.
The first two articles respectively describe about JVM, JDK, JRE, collection, classloader, and some design
Pattern self-understanding. We are still not prepared to start the transition to J2EE this time, because we think there are still some trivial j2se problems that have not been summarized.

1.Object Class understanding
We all know that object is the base class of all Java classes,This means that all Java classes inherit the 11 methods of the object. We recommend that you check the object
The source code of the 11 member functions will know the default implementation method. For example, in the equals method, the default implementation is to use "=" to compare, that is, to directly compare the memory address, return trueOrFalse. The tostring () method returns the string composition method ??
"Getclass (). getname ()+"@"
+Integer. tohexstring (hashcode ())"
In fact, you can understand the composition of this string without having to explain it too much. Next let's take a look at hashcode ():
PublicNativeIntHashcode ();

Because the native method is related to the OS processing method, there is only one declaration in the source code. If we are interested, we can fully explore how its hashcode is generated by the OS? However, I suggest that you remember to use the principles first! First, if the same object of the equals () method has the same hashcode,
() When the object is inconsistent, it is not guaranteed that the hashcode () method returns different integers. In addition, when the same program is run next time, the same object may not be the original hashcode ()
Oh.
What about other methods? Nofigy (), policyall (), clone (), and wait () are all native methods, which indicate that they depend on the implementation of the operating system. The last interesting method is finalize (). Similar to the C ++ destructor, the signature is protected, which proves that the method can be used only after the inheritance extension, and the method body is empty, do nothing. It serves to notify the JVM that the object is no longer used and can be destroyed at any time. The actual destruction right lies in the virtual machine. So does it really do nothing? Not necessarily, in fact, if it is a thread object, it will lead to a higher priority of the thread within a certain range, leading to faster destruction to save memory and improve performance. In fact, from a common sense, we can probably guess the purpose of JVM practice.

2.Relationship Between Heavy Load hashcode () and collection framework
I once heard from a senior who has been engaged in Java training for many years that in his opinion, the hashcode method has no significance, just to prove that having the same hashcode will lead to equalsMethods exist in the same way. Even some predecessors have made such a mistake, which means it is easy to be ignored. So what is the purpose of the hashcode () method?

Related Article

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.