JAVA basics-I fully understand JAVA

Source: Internet
Author: User

The following are some key points of Java standard technology:

I. cross-platform Java, that is, a compilation is run everywhere

To put it simply, the cross-platform nature of Java means that compiled Java programs can run directly on different platforms without re-compiling, this feature makes Java become increasingly popular with the popularity of Web applications. How is Java cross-platform implemented? This requires understanding the concept of Java Virtual Machine and bytecode.

The compiled Java code is not a traditional binary code (such as a windows. EXE file), but a Java bytecode. Such bytecode files cannot be directly executed on the operating system. To run a Java program on an operating system, there must be an intermediate link to interpret the Java bytecode as a binary code, which is the Java Virtual Machine (JVM ). Currently, most operating systems have implemented JVM, so Java is easy to implement cross-platform.

Ii. Face Image Object Technology

Java fully supports the face-to-face object technology. This is reflected in that Class is the basic unit of Java programs. A Java program is usually composed of many classes, in addition, these classes have certain inheritance relationships. Java supports Class single inheritance, so that the inheritance relationships between classes are clearer. The result of inheritance produces a class polymorphism. In essence, the class polymorphism can be used to reference the parent class to access the implementation of the inherited class (subclass object ), class polymorphism forms the foundation of the component object model, that is, the interface (parent class) Access Implementation (subclass ).

Iii. I/O operations in Java

In Java, binary data and character data are read and written using byte streams (InputStream and OutputStream) and delimiter streams (Reader and Writer. The File class in the Java class library not only provides File operations but also contains folder operations. For example, the following lines of code can list all the files under the C-drive root directory:

File f = new File ("c ://";
String [] m_dir = f. list ();
For (int I = 0; I System. out. println (m_dir );

4. Graph and event processing in Java

You can use the awt package or the Java class of the swing package to design a majority of Java graphical interfaces. The following lines of code will generate a 200*200 pixel form:

Frame f = new Frame ("Welcome ";
F. setSize (200,200 );
F. setVisible (true );

By default, the close button of the Frame form does not work, which is also confusing for beginners in Java. To enable the Frame form to be closed when you press the close button, you need to make the form respond to a wide wevent event. The specific method is to add an event listener object to the form, this event listener is the implementation of the WindowListener interface. In the above Code, insert the following code to close the form:

F. addWindowListener (new WindowAdapter (){
?? Public void windowClosing (WindowEvent e ){
System. exit (0 );
??}
}

An anonymous internal class is used here. An anonymous internal class is a flexible way to use internal classes in Java.

V. Java thread and Synchronization Control

The thread concept is introduced to achieve parallel processing and thus improve program efficiency. The Thread implementation in Java is very simple. You can use two methods to create a Thread, one is to implement the Runnable interface, and the other is to inherit the Thread class to override the run () method. The only difference between the two methods is that the former retains the possibility of inheriting a class (Because Java only supports class single inheritance, but the interface does not have this restriction ).

The start () method is always used to start a thread. run () in the Thread class can be called directly, but it is never used to start a thread. there is a fundamental difference between the two.

The synchronized keyword is used to protect sensitive data in the thread. The content in the synchronized block can be accessed by only one thread at a time. Therefore, the data in the synchronized block is thread-safe.

You can use the wait () and notify () methods in the Object class to implement interaction between threads, but remember the wait () and notify () methods () only when the method occurs on the same object can the interaction between threads be realized. The thread blocked by the wait () method of an object needs another thread that calls notify () of the same object to resume running. The Y () method wakes up a thread blocked by the wait () method at a time. The yyall () method can wake up all threads blocked by the wait () method at a time.

6. Implementation of the native method in Java

Java is not perfect. In addition to its shortcomings, Java is much slower than traditional C ++ in terms of running speed. In addition, Java cannot directly access the underlying operating system (such as system hardware ), therefore, Java uses the native method to extend the functions of Java programs.

The native method can be compared to the interface of a Java program and a C program. The implementation steps are as follows:

1. Declare the native () method in Java and compile it;
2. Use javah to generate a. h file;
3. Write a. cpp file to implement the native export method, which must contain the. h file generated in step 2 (note that it contains the jni. h file in JDK );
4. Compile the. cpp file in step 3 into a dynamic link library file;
5. Use the System. loadLibrary () method in Java to load the dynamic link library file generated in Step 4. This native () method can be accessed in Java.

Some of the Java technologies mentioned above have a certain degree of universality. They are basically the skills that need to be mastered in all aspects of Java. In fact, Java is widely used, and every aspect must follow different specifications. The following is a brief introduction to Java applications.

(1) understand the three Java SDK versions:

Java SDK Micro Edition (j2s)

It is used to develop applications used on mobile communication devices such as handheld computers and mobile phones. Not all mobile devices support Java. Only devices that have the j2_runtime environment (JVM + j2_api) can run Java programs. The integrated development tools (usually with some true accessors) of j2's include Sun's j2rwireless Toolkit and IBM's megal Age Micro Edition.

Java SDK Standard Edition (J2SE)

It is mainly used to develop general desktop applications. JDK is usually referred to as J2SE, and we started learning Java from J2SE.

Java SDK Enterprise Edition (J2EE)

It is used to develop distributed enterprise-level large-scale applications. The core is the development of Entetprise Java Beans (EJB, distributed Java component.

(2) Java Applet)

A Java Applet is a Java class that inherits the Applet Class and overwrites methods such as init (), paint (), and stop (). It is deployed on Web servers (such as IIS) when the client requests a Web page, the browser downloads it from the Web server to the local client. Then, the browser creates an instance of the Applet Class and calls its init () method, in terms of security, the Applet does not have the permission to access local files. Because the Applet is executed by the browser, the Applet does not need a main () method. In fact, all Java applications except Java applications do not need a main () method.

(3) server-side Java applets (Servlet)

Servlet is also a Java class. Compared with Applet, Servlet is a Java Applet running on the server, and Servlet needs a separate Web server (such as Tomcat) as a container. In addition, some classes (such as HttpServlet) used in the Servlet are not included in the J2SE API. Therefore, you need to add Servlet. jar (in the Tomcat commonlib folder) to the environment variable. The following is a simple Servlet example:

Public class Myservlet extends HttpServlet {

?? Public void doGet (HttpServletRequest request, HttpServletResponse response)
{
?? Try {
Response. setContentType ("text/html ";
PrintWriter out = response. getWriter ();
Out. println ("");
Out. println ("");
Out. println ("Hello world ");
Out. println ("");
Out. println ("");
??} Catch (IOException e ){}
}
}

Compile the Class file and put it under TomcatwebappsexamplesWEB-INFclasses. Then, enter http: // 127.0.0.1 in the address bar of the browser: 8080/examples/servlet/Myservlet you can see Hello world appears in the browser.

(4) Java Server Page (JSP)

Similar to Servlet, JSP runs on the Web server and requires containers such as Tomcat. The difference is that JSP embeds Java code in html markup (like ASP, with <%... %>). The JSP interface design can be effectively separated from the work of background developers. It can be imagined that it is difficult for developers to write a Web page with Servlet. Therefore, JSP + Servlet Hybrid Web application is an ideal choice.

It seems that JSP is similar to ASP implementation mechanism, but there are also essential differences. All ASP pages are interpreted and run, while JSP pages are compiled upon the first request, and client requests are directly run on the server. class file (in the Tomcat Work folder), JSP is much faster than ASP.

(5) Java Beans

Java Bean is a reusable component and there is no strict specification for Java Bean. Theoretically, any Java class can be a Bean. However, since Java Beans are created by containers (such as Tomcat), Java Beans should have a constructor without parameters. In addition, generally, Java Bean must implement the Serializable interface to realize Bean persistence.

(6) Enterprise Java Beans (EJB)

Java Bean is actually equivalent to the COM component in the local process in the Microsoft COM model. It cannot be accessed across processes. Enterprise Java Bean is equivalent to DCOM, which is a distributed component. It is based on Java remote method call (RMI) technology, so EJB can be remotely accessed (cross-process, cross-computer ). However, EJB must be deployed in containers such as Webspere and WebLogic. The EJB client never directly accesses the real EJB component but accesses it through its container. The EJB container is the proxy of the EJB component. The EJB component is created and managed by the container. The customer accesses the real EJB component through the container.

This model is similar to the COM + manager. In fact, the EJB container serves as the COM + manager, but the EJB component is easier to use and safer than the COM component.

In general, Java, as a representative of the face object technology, is more likely to develop efficient, multi-layer distributed applications in today's commercial applications, java has strong robustness and ease of use. Combined with the combination of UML applications, the development cycle of a commercial application software will be greatly shortened, so Java will have a good prospect.

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.