Summary of Java Fundamentals (i)

Source: Internet
Author: User

1. String,stringbuffer,stringbuilder

1). Any changes to the string object do not affect the original object, and any related change operations will generate a new object

2). The StringBuilder and StringBuffer classes have the same member properties and member methods, except that the StringBuffer class has one more keyword in front of the member method: synchronized

3). For the direct addition of strings, the efficiency is high, because the compiler determines its value, that is, the shape of "I" + "Love" + "Java"; Strings are added, and are optimized for "Ilovejava" during compilation. This can be decompile with the javap-c command.

The generated class file for validation. For the indirect addition (that is, contains the string reference), the shape is like s1+s2+s3; Efficiency is lower than the direct add, because the compiler does not optimize the reference variable.

4). Execution efficiency of String, StringBuilder, StringBuffer: StringBuilder > StringBuffer > String

Of course this is relative, not necessarily in all cases. For example, string str = "Hello" + "world" is more efficient than StringBuilder st = new StringBuilder (). Append ("Hello"). Append ("World") is higher.

Therefore, these three classes are each with pros and cons and should be used according to different circumstances:

It is recommended to use string str= "Hello" in the case of a string addition or less modification;

It is recommended to use StringBuilder when the string addition operation is large, and stringbuffer if multi-threading is used.

5). "Hello" +2 has been optimized to "Hello2" during compilation; because of the existence of a symbolic reference, String C = b + 2; is not optimized during compilation and does not treat b+2 as literal constants;

For a final modified variable, a copy is saved in the class file constant pool, that is, not accessed through a connection, and access to the final variable is directly substituted for the actual value during compilation. So string c = B + 2;

will be optimized during compilation: String c = "Hello" + 2;

Although B is decorated with final, but because its assignment is returned through a method call, its value can only be determined during run time

String str = new String ("ABC"): During class loading, it is true that an "ABC" object is created in the run-time pool, and indeed only one String object is created during code execution.

For more information, please read: http://www.cnblogs.com/dolphin0520/p/3778589.html

2. Hashcode ()

1). If the result of calling the Equals method is true, the hashcode value of the two objects must be equal, and if the Equals method results in False, the hashcode value of the two objects is not necessarily different; if the hashcode value of two objects

Unequal, The result of the Equals method must be false, and if the hashcode value of two objects is equal, the result of the Equals method is unknown.

2). While overriding the Equals method, you must override the Hashcode method


3. Automatic box packing and unpacking:

1). The valueof (int) method of the integer is automatically called when boxing. The Intvalue method of integer is called automatically when unpacking.

2). When you create an integer object by using the ValueOf method, if the value is between [-128,127], a reference to the object that already exists in Integercache.cache is returned, otherwise a new integer object is created.

3). The valueof method of the double class takes a different implementation than the ValueOf method of the integer class. It is simple: the number of integer values in a range is limited, but floating-point numbers are not.

4). An Integer, short, Byte, Character, Long is similar to the implementation of the ValueOf method for several classes. The implementation of the valueof method of Double and float is similar.

5). Boolean I1 = false; Boolean i2 = false; System.out.println (I1==I2); The print result is true. There are two static member variables defined in a Boolean.

public static Final Boolean true = new Boolean (true);

public static Final Boolean false = new Boolean (false);

6). When the two operand of the "= =" operator is a reference to the wrapper type, the comparison points to whether the same object is the same, and if one of the operands is an expression (that is, the arithmetic operation is included), the value is compared (that is, the process that triggers the automatic unboxing).

7). Talk about the integer i = new integer (XXX) and integer i =xxx; the difference between the two methods. Of course, this topic belongs to a relatively broad category. But the main point must be answered, I summed up the following two points are the difference:

A. The first method does not trigger the automatic boxing process, while the second method triggers;

B. Differences in implementation efficiency and resource occupancy. The second approach is more efficient and resource-intensive than the first case in general (note that this is not absolute).

4. = = and Equal

1). for = =, if the variable that is acting on the base data type is directly compared with the value of its stored values, or if it is acting on a variable of the reference type, the address of the object being pointed to is compared

2). For the Equals method, note that the Equals method cannot act on a variable of the base data type. If you do not override the Equals method, you are comparing the address of the object to which the variable of the reference type points, such as String, date, and so on.

If the Equals method is overridden, it compares the contents of the object being pointed to.

5. Access Modifiers:

1). Modifier class:

A. Default access rights (Package access): Used to decorate a class, indicating that the class is visible only to other classes in the same package.

B. Public: Used to modify a class to indicate that the class is visible to all other classes.

2). Methods and variables that modify the class

A. Default access rights (Package access rights): If a method or variable of a class is decorated with a package access permission, it means that the method or variable of the class can be called only in other classes in the same package, and that the class cannot be called in a class in a different package.

method or variable.

B. Private: If a method or variable of a class is modified by private, then the method or variable of the class can only be accessed in the class itself, and cannot be accessed outside the class or in any other class.

C. Protected: If a method or variable of a class is protected decorated, the class's methods or variables can be accessed for the same package class. For classes of different packages, only classes that inherit from the class can access methods or variables of that class.

D. Public: Methods or variables that are modified by public are visible everywhere.

6. JVM Memory Area partition:

1). First the Java source code file (. java suffix) is compiled by the Java compiler into a bytecode file (. class suffix), and then the class loader in the JVM loads the bytecode files for each class, and after loading, it is done by the JVM execution engine. Throughout the program execution

process, The JVM uses a space to store the data and related information that is needed during the execution of the program, which is generally referred to as the runtime data area (runtime), which is what we often call JVM memory.

2). Program Counter Register: Also known as PC Register. Although the program counters in the JVM are not the same as the CPU registers on the physical concept, like the program counters in assembly language, the program counters in the JVM

function and The function of the program counter in assembly language is logically equivalent, that is, to indicate which instruction to execute. In the JVM specification, if a thread executes a non-native method, the program counter holds the current required

The address of the executed instruction; if the thread is executing the native method, the value in the program counter is undefined.

Because the size of the space that is stored in the program counter does not change with the execution of the program, there is no memory overflow phenomenon (OutOfMemory) for program counters.

3). Java stack: A stack frame is stored in the Java stack, each stack frame corresponds to a called method, including the local variable table (local Variables), the operand stack (Operand stack) in the stack frame, and a run-time pool that points to the class to which the current method belongs

(The concept of running a constant pool is referred to in the Method area section) (Reference to runtime constant pool), the method return address, and some additional additional information. When a thread executes a method, it is created with the

a corresponds to the stack frame and will build up the stack frame to press the stack. When the method finishes executing, the stack frame is stacked.

4). Local method Stack: The local method stack and the Java stack function and principle are very similar. The difference is simply that the Java stack is for the Java Method service, while the local method stack serves the local method (Native). In the JVM specification,

and no to the local the concrete implementation method of the method stack and the data structure are mandatory, and the virtual machine can implement it freely. The local method stack and the Java stack are directly merged into the hotsopt virtual machine.

5). Heap: The heap in Java is used to store the object itself as well as an array (of course, the array references are stored in the Java stack).

6). Method Area: The method area is also a very important area in the JVM, which, like the heap, is a thread-shared area. In the method area, information is stored for each class (including the name of the class, method information, field information), static variables, constants, and

compiler compiled code, and so on. in the class file, in addition to the description of the fields, methods, interfaces, and so on, there is a constant pool that stores the literal and symbolic references generated during compilation.

A very important part of the method area is the run-time pool, which is the runtime representation of the constant pool of each class or interface, which is created when classes and interfaces are loaded into the JVM. Not a class file, of course.

content in a constant pool in order to enter the run-time pool, you can also put new constants into the run constant pool, such as the Intern method of string, during runtime.

7. How to effectively avoid oom: good at using soft and weak references

1). Strong references (Strongreference): Strong references are common in program code, so long as an object has a strong reference associated with it, the JVM must not reclaim the object, even if the JVM prefers to throw

OutOfMemory Error This object is not recycled.

2). Soft Reference (SoftReference): Soft references are used to describe some useful but not necessary objects, which are represented in Java by the Java.lang.ref.SoftReference class. For objects associated with soft references, only when the memory is low

The JVM will not recycle the object. Therefore, this is a good way to solve the problem of oom, and this feature is well suited for caching: such as Web caching, image caching, and so on.

3). Weak references (WeakReference): Weak references are also used to describe non-required objects, and when the JVM is garbage collected, the objects associated with the weak references are reclaimed, regardless of sufficient memory. In Java, use the Java.lang.ref.WeakReference

class to represent.

4). Virtual Reference (phantomreference): The virtual reference differs from the previous soft reference and weak reference, and it does not affect the life cycle of the object. Represented in Java with the Java.lang.ref.PhantomReference class. If an object is associated with a virtual reference,

then with no reference associated with it, it can be recycled at any time by the garbage collector.

Summary of Java Fundamentals (i)

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.