Basic Java Learning (i)

Source: Internet
Author: User

How Java Works

source Program (Myprogram.java) –> compile (Javac myprogram.java), Java bytecode (Myprogram.class), run (Java myprogram)

Instructions:

    • Compile time: javac (compiler) + file name
    • Run time: Java + file name

Program Structure of Java

  Source Files > Classes > Methods > statements (source File > class > method > Statement)

  

import java. Lang. String; import java. Lang. System; public class Myfirstapp { public void main(String[] args){    System. Out. Print("Hello world!" );    }}

Attention:

    • FileName Myfirstapp Be sure to have the same name and case sensitivity for the class within the file.
    • The case is not sensitive when compiling with the javac instruction.
    • Case sensitive when running with Java directives.
    • Import Java.lang can be omitted
    • When the program starts, it goes to the main () method, and main () is the starting point for the program.
    • 1 programs available and as of 1 Mian () methods
    • 1 Java programs have at least 1 classes, but can be multiple
    • Java is a strongly typed, conditional test result must be a Boolean value

The differences between JDK, JRE, JVM:

    • JVM (Java Virtual machine): Java VM
    • JDK (Java Developer ' s kit): Java Development Kit
    • JRE (Java Runtime Environment): Java Runtime Environment

Classification of programming languages (by Program execution mode):

  1. Compiled language: A specialized compiler is used to translate source code into machine instructions that can be recognized by a computer for a specific operating system. such as C, C + +

2. Interpretive language: A special interpreter is used to interpret the source code as a specific machine instruction, explaining an execution sentence, similar to simultaneous translation. such as ASP, PHP.

JVM first knowledge and working principle:

  

The JVM consists of four main parts:

      1. Class loader (ClassLoader): Loads the required class into the JVM when the JVM is started or when the class is running.

      2. Execution Engine: responsible for executing bytecode directives contained in the class file

      3. Memory area (also called run-time data area): operates the allocated memory area when the JVM is running. The runtime memory area can be divided into 5 regions,

        

      

    • Method Area: A place for storing class structure information, including constant pools, static variables, constructors, and so on. Although the JVM specification describes the method area as a logical part of the heap, it has an individual name of non-heap (not a heap), so let's not confuse it. The method area also contains a run-time-constant pool.
    • Java heap: The place where Java instances or objects are stored. This block is the main area of the GC (explained later). From the stored content we can easily know that the method area and heap are shared by all Java threads.
    • Java stack: the Java stack is always associated with threads, and whenever a thread is created, the JVM creates a corresponding Java stack for that thread. In this Java stack will contain more than one stack frame, each run a method to create a stack frame for storing local variable table, Operation Stack, method return value and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the Java stack into the stack to the process. So the Java stack is ready-made and private.
    • Program counter (PC Register): Used to save the memory address of the current thread execution. Because the JVM program is multi-threaded execution (thread rotation switching), in order to ensure that the thread switch back, you can return to the original state, you need a separate counter, record where the previous interruption, visible program counters are also thread-private.
    • Local method Stack (Native): Similar to the Java stack, except for the Native method used by the JVM

    4. Local method Interface: The main is to call the C or C + + implementation of local methods and return results.

  The JVM is at the bottom of the entire JDK and is responsible for interacting with the operating system, shielding the operating system environment, providing a stony Java runtime environment, and therefore also known as a virtual machine. The operating system is loaded into the JVM through the Java.exe in the JDK, mainly in the following steps:

1. Creating a JVM load environment and configuration

2. Loading Jvm.dll

3. Initialize Jvm.dll

4. Invoking the JNIEnv instance to load and Process class classes

5. Running Java programs

What is GC,GC's working principle: Garbage Collection, Garbage collection

1. The garbage collector generally has to do two things: detect garbage, recycle garbage. How to detect the rubbish? There are generally several ways to do this:

Reference counting method: To add a reference counter to an object, whenever there is a place to refer to it, the counter is incremented by 1, and the reference fails by 1.

Accessibility analysis algorithm: The root set object as the starting point to search, if the object is not accessible, that is, garbage objects. The root set here typically includes objects referenced in the Java stack, objects referenced in the method area constant pool, objects referenced in local methods, and so on.

2. Why use a generational garbage collection strategy?

In the process of running Java program, will produce a large number of objects, because each object can assume different functions have different function so there is a distinct life cycle, some object life cycle is longer, such as the Session object in the HTTP request, thread, socket connection, etc. Some objects have shorter life cycles, such as String objects, which can be recycled once they are used because of their invariant class properties. Imagine that, without the distinction between object survival time, each garbage collection is to reclaim the entire heap space, then the time spent is relatively long, and for long-lived objects such as scanning work is futile. Therefore, it is necessary to introduce the idea of divide and conquer, the thought of the so-called divide-and-conquer is to adapt the object to the division of the objects, and put different life cycle objects on different generations to use different garbage collection methods.

  

3. How to divide?

The objects are divided into the different life cycles: young Generation, Old Generation, and Permanent Generation, for generations. In which the persistence of the main storage is the class information, so the recovery of Java objects is not very important, and recycling is closely related to the younger generation and the elderly generation.

4. Generational recycling is as follows:

Basic Java Learning (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.