Java Basics of Senior Java Review notes

Source: Internet
Author: User
Tags bitwise

First, Static and final

Learning Java for so long, as if they did not use the final, so the understanding of fianl is not enough. Final appears not only in the modification of variables, but also in methods and classes. The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. The final method locks the method, and the subclass cannot overwrite the modification method, and the method is efficient. The member variable of the final variable final modification represents a constant, can only be assigned once, the value cannot be changed after the assignment, final means that the referenced variable cannot be changed, but the contents of the object pointed to by the reference can be changed, and the final variable can be assigned one time if it is null.

Second ". Java" files can contain multiple classes

There can be multiple classes, but there can be only one class of "public", and the name is the same as the file name.

Third, & and &&

Both & and && can be used as operators of logical operations. However, the && will appear short-circuit function, that is, if the previous conditions are not established, the following conditions will not be executed, for example: (1==2&&j++>1).

& can also be used as a bitwise operation,& to indicate bitwise AND operations (two digits of the same bit are 1, or 1; if one is not 1, then 0).

Four, integers and int

int is one of 8 primitive data types in Java. Java provides a wrapper class for each raw data, and the integer is the wrapper class provided by Java for Int. The default value for int is 0, and the default value for integer is null.

V. Creation of String in Java

String in Java is a special wrapper class data that has two forms of creation:

1, String s = "abc";

2, String s = new String ("abc");

The first one creates an object reference variable s in the stack to the string class, and then goes to find if "abc" is stored in the string constant pool, and if not, creates three char values ' a ', ' B ', ' C ' in the stack, and then creates a string object in the heap, Its value is an array of three char values just created in the stack {' A ', ' B ', ' C '}, then the string object is stored in the string constant pool, and finally the s to the address of the object, if "ABC" has been saved in the string constant pool, The object with the value "ABC" is found in the string constant pool and the S is pointed to the address of the object.

The first feature: the JVM will automatically determine if it is necessary to create a new object based on the actual data in the stack.

The second type can be decomposed into two steps 1, String object = "abc"; 2, String s = new String (object); The first step is to refer to the first method of creation, and the second step because "ABC" has been created and saved to the string constant pool, so the JVM will only create a new string object in the heap whose value shares the three char values already in the stack.

The second feature is to create new objects in the heap, regardless of whether their string values are equal or not, and whether it is necessary to create new objects.

Vi. Error and exception

Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations. Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.

Seven, sleep () and wait ()

Sleep is the thread that is executing the active cpu,cpu to execute other threads, the CPU will go back to this thread after the time specified by sleep, and if the current thread enters a sync lock, the Sleep method does not release the lock. Even if the current thread uses the Sleep method to give out the CPU, other threads that are blocked by the sync lock cannot be executed. Wait refers to a thread that has entered a sync lock, allowing itself to temporarily let out a sync lock so that other threads waiting for the lock can get a synchronous lock and run, and only other threads call the Notify method (notify does not release the lock, Just tell the thread that called the wait method to be able to participate in the competition to get the lock, but not immediately get the lock, because the lock is still in the hands of others, others have not been released. If the code behind the Notify method is much more, it needs to be executed before releasing the lock, you can add a wait and some code after the Notfiy method, see the effect), the thread that calls the wait method will dismiss the wait state and the program can continue to run down after the lock is received again.

Eight, run () and start ()

In Java, start () is the startup thread, and run () is the method that executes after the thread is started.

Ix. ArrayList and vectors

1, the synchronization of

Vector thread safe, ArrayList non-thread safe.

Note: For Vector&arraylist, Hashtable&hashmap, to remember the problem of thread safety, remember that the vector and Hashtable are old, Java is a birth to provide, they are thread-safe, ArrayList and HashMap are provided at JAVA2, and they are thread insecure. So, we talk about the old first class.

2. Data growth

Both ArrayList and vectors use an initial capacity size, and when the storage is out of size, the vector grows as much as it did, and the ArrayList grows 0.5 times times the original.

X. List and map

One is a collection of single-column data, and the other is a collection of two columns of data that store keys and values. Lists are sequential and allow duplicates; the data stored in the map has no order and its key values do not allow duplicates, but the values are allowed to be duplicated.

Xi. ArrayList, vectors and linklist

Both ArrayList and vectors store data using an array method. The number of elements in this array is greater than the actual stored data in order to increment and insert the element. They allow the element to be indexed by ordinal, but the insertion element also involves memory operations such as array element movement, so the index is fast and the insertion is slow. Vector because of the use of thread safety, so efficiency is relatively arraylist poor, in fact, on the new JVM their efficiency is not small. Linklist uses two-way linked list to achieve storage, indexed by ordinal data needs to be forward traversal or backward traversal, but when inserting data, as long as the record before and after the item data can be, so the insertion speed. Linklist is non-thread safe.

12. Head and Stack

Java's memory is divided into two categories, one is stack memory, and the other is heap memory. Stack memory refers to the method when the program enters the method, a separate private storage space is allocated for this approach, which is used to store local variables inside the method. When this method ends, the stack allocated to the method is released, and the variables inside the stack are freed.

Heap-to-stack memory, typically used to hold data that is not placed in the current method stack, such as objects created with new are placed in the heap, so it will not disappear with the end of this method. The local variables in the method are placed in the heap instead of the stack, using the final decoration.


Java Basics of Senior Java Review notes

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.