Java Common knowledge points (Mian Shi Ti) accumulation

Source: Internet
Author: User
Tags shallow copy stringbuffer

1. String, StringBuffer, StringBuilder three differences?

(1) Comparison of the three in the execution rate: String<stringbuffer<stringbuilder
Reason: String is a constant of strings and Stringbuffer,stringbuilder is a string variable; Whenever strings are manipulated by string, the new objects are actually created constantly, and the original objects become garbage collected by GC, whereas StringBuffer and StringBuilder are string variables, which are objects that can be changed, and when they are used to manipulate strings, is actually manipulated on an object.


(2) Comparison of StringBuffer and StringBuilder
StringBuilder is not thread-safe, and StringBuffer is thread-safe. StringBuilder is preferred in single-threaded application scenarios because it is faster.

2. What is the difference between "= =" and Hashcode () equals?

(1) Basic use:
"= =" is an operator that compares two variables for equality.
Equals is a method of the Objec class that compares two objects for equality, and the Equals method of the Default object class is to compare the addresses of two objects, as with the result of = =;
Hashcode is also a method of the object class. Returns a discrete integer of type int. Used in collection class operations in order to improve query speed.

(2) What to do when placing an object in a collection:
First, determine whether the hashcode value to put into the object is equal to the hashcode value of any of the elements in the collection, and if not, put the object directly into the collection.

If the hashcode value is equal, then the Equals method is used to determine if any of the objects in the collection are equal, and if equals does not determine equality, the element is placed directly into the collection, otherwise it is not put.

(3) Note also: When you overwrite equals, you always overwrite hashcode.

3. What are the methods of the object class

(1) Clone method
Implement the shallow copy of the object, only implement the Cloneable interface can call the method, otherwise throw clonenotsupportedexception exception.
(2) GetClass method
Used to obtain the run-time type.
(3) ToString Method
Returns a string
(4) Finalize method
Used to free resources, but is seldom used because it is not possible to determine when the method is called.
(5) Equals method
Used to compare the equality of two objects
(6) Hashcode method
For hash lookups, overriding the Equals method generally overrides the Hashcode method.
(7) Wait method
Causes the current thread to wait for the lock on the object, and the current thread must be the owner of the object, which is the lock that owns the object. The wait () method waits until the lock is acquired or interrupted.
(8) Notify method
Wakes a thread waiting on the object.
(9) Notifyall method
Wakes the thread waiting on the object.

Java Common knowledge points (Mian Shi Ti) accumulation

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.