Java knowledge point Summary 1

Source: Internet
Author: User

1 can a char type in Java store one Chinese character? Why?

Yes. Because the minimum unit of JVM is two bytes, while Java stores Chinese Characters in UNICODE, Unicode occupies two bytes.

2 What are the methods for implementing multithreading in Java? How to implement synchronization?

There are two methods to implement multithreading: 1. Extend the Thread class; 2. Implement the runnable interface.

There are two ways to implement synchronization: 1. Add sychronized BEFORE THE METHOD

2. Call wait (), Y (), or notifyall () in the method ();

3. abstract classes and interfaces

Abstract class: at least one class with incomplete methods is called abstract class. abstract classes cannot be instantiated. classes that inherit this abstract class can be abstract classes or non-abstract classes. abstract classes can be used to implement public code.

Interface: pure abstract class. All of its methods are not implemented. abstract classes are used to summarize common behaviors.

One class can implement multiple interfaces, but only one class can be extended. The interface cannot expand other classes.

When to use abstract classes? When to use interfaces?

An interface is used to define a behavior model, while an abstract class is used to implement public code.

Harvest 1:
When using the preparedstatement class. I need to set a time variable for it. Save year, month, and day. I have never realized this problem. Only setdate (index. java. SQL. Date) in preparedstatement can be processed. Only java. SQL. date is a subclass of Java. util. Date. Save the date in the format of yyyy-mm-DD in the database. Never paid too much attention. Now you want to save the time, minute, and second. To process it.
But how do I set them to year, month, and day. Later, I checked the JDK source code to find out that this date class can only save year, month, and day. I found another settime (index, java. SQL. Time) class. This time. But not yet. The result is only; hour, minute, and second, but not year, month, or day. Only by looking at the source code can we know that he is also a java. A subclass of util. Date. Only save time, minute, and second.
I'm depressed. Isn't there a complete date saved. Then I found one. Settimestamp (index, java. SQL. timestamp), oh, this java. SQL. timestamp should be fine. Good. He does. This time, I also took a look at the source code of the Java. SQL. timestamp class. Good. Today I got to know these classes.

Harvest 2:
Today, we found a major habitual error in writing a loop processing:
The Code is as follows:
For (Int J = 0; j <itemlist. Size (); j ++ ){
Itemloginform item = (itemloginform) itemlist. Get (j );
String name = form. getform_id () + "_" + item. getitem_code ();
If (request. getparameter (name )! = NULL)
Item. setitem_value (request. getparameter (name ));
Else
Itemlist. Remove (j); // Here it should be itemlist. Remove (j --);
}
The idea is simple, that is, to filter the set. Remove a file that does not meet the conditions. However, problems are found during the test. Some elements do not participate in judgment. Depressed. After reading it for a long time, I discovered it. The list set is used to process its elements. Perform the remove operation. The size is automatically reduced. All elements are moved forward. The value of variable J has not changed, so the next element is missed. It is really hard to find out. If j -- is used to remove an element, it can be sequentially performed from the next element.
This error also makes me clearly understand many details and should be well understood to reduce the occurrence of such bugs.

 

1. The main workspace and thread workspace in the JVM. The main workspace is shared by all the threads of the instance, and the thread workspace is the dedicated workspace of each thread of the instance, including copying of some instance field data in the main workspace.

2. The server generally has a thread pool, and the thread resources can be reused. Your 2000 users are online, and you may not be able to access another 200 users simultaneously (or concurrently. Besides, as long as the object is not too large, I would rather use 200 copies than waiting in the queue of the 200 tasks.

3. Copy data between two databases. Each database uses its own sequane to generate IDS. During data replication, if the foreign key in the DB is maintained by the DB, no ID conflict occurs. If the foreign key is maintained by the external program, an error may occur!

4. for data members of non-static classes, there is a copy of the instances generated by the class and they are independent of each other (The modification does not affect other instances ), however, the static data member is changed to only one copy of each class, that is, all instances generated by this class share this data member, and the change of this data member will affect other instances. the static method does not need to create an object or call this method.

5. threadlocal is used to place reference of frequently used objects in a bucket of the thread itself. During the thread's execution, you can easily obtain this object through static threadlocal of the class, instead of passing through parameters.

 

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.