Getting Started with Java

Source: Internet
Author: User

Getting Started with Java

①. Bytecode is interpreted by the Java Virtual machine to run, that is, the Java Virtual Machine is responsible for translating bytecode into the machine code of the local computer, and will give the machine code to the local operating system to run.

②. If there are multiple classes in the source file, only one class is public, and if a class is a public class, the name of the source file must be exactly the same as the name of the class, with the extension. java;

③. If there is no public class in the source file, the source file name is the same as the name of a class, and the extension is. java.

④. If the source file contains more than one class, compiling the source file generates multiple files with the. clalss extension, and each file with a. class extension is a byte code of only one class with the same name as the class.

⑤. These bytecode files are stored in the same directory as the source file. The class name that the Java interpreter executes must be the name of the main class (without the extension). When running an application using the Java interpreter, the Java Virtual machine first loads the bytecode file required by the program into memory and then interprets the execution of its own code file.

Basic data types and arrays

There is no unsigned byte,short,int and long in Java, which differs greatly from the C language, so unsigned int m; is the wrong variable declaration.
Base type level from high to low

Double    float    Long int    Char     Short byte

When assigning the value of a low-level variable to a variable with a high level, the system automatically completes the conversion of the data type. A type conversion operation must be used when assigning the value of a high-level variable to a variable of low level. A common mistake is to assign a double type constant to the float type variable without a type conversion operation: float x = 12.4;
The right approach:

float x = 12.4F; or float x = (float) 12.4;

Note: Java does not allow you to specify the number of array elements within the square brackets in the declaration array, unlike C + + +. Declare multiple arrays at once: int [] A, B;

Note: Unlike the C language, Java allows you to specify the number of elements of an array using the value of the int variable, for example: int size = 30;double number[] = new Double[size];

Classes and objects

The member variable has a default value, but the local variable has no default value, so you must ensure that the local variable has a specific value before using the local variable.

Note: Assignment of variables

assign an initial value when declaring a variable class a{int  A; float b= 12.56f;} Illegal assignment statement: Compile error class  a{int  A; float b;a=12; b=12.56f;}

The so-called creation of objects refers to assigning variables to objects and obtaining a reference to ensure that these variables are manipulated by the object, and that the new operator can only operate with the constructor of the class, and the final result of the operation is a hexadecimal number, which is called the reference to the object, the expression new The value of XXX () is a reference. Before the new operator computes this reference, it first allocates memory space to the member variables in the class, and then executes the statements in the constructor, which cannot be called when the object is born, because no reference has been computed, that is, the member variable whose memory space is allocated is a member of "who". When a reference is computed, the new XXX () expression already has a value, and the object is born. If you assign the value of the new XXX () expression to an object, the object has a member variable that is allocated memory by the new operator, that is, the new operator assigns a variable to the object.

Note: The object's reference exists in the stack, and the object's entity (the variable that allocates the object) exists in the heap.

Stacks and heaps (heap) are places that Java uses to store data in RAM. Java automatically manages stacks and heaps, and programmers cannot directly set stacks and heaps. The advantage of the stack is that the access speed is faster than the heap. The disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that Java's garbage collection will automatically take away the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

Garbage collection

The Java runtime's garbage collection discovers that the allocated entity in the heap is no longer referenced by any object in the stack, freeing the memory that the entity occupies in the heap on the stack. As a result, there is little "memory leak" in Java, which is the memory overflow caused by the program forgetting to free up memory.

Note: If you want the Java virtual machine to be "garbage collected" immediately, you can have the system class call the GC () method.

Getting Started with Java

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.