The distinction of key keywords in Java

Source: Internet
Author: User

 1, final, finally, finalize the difference


final-modifier (keyword) If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class.

Therefore, a class cannot be declared abstract and declared final.
declaring variables or methods as final ensures that they are not changed in use.
A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified.
the method that is declared final is also used only and cannot be overloaded.
Finally-provides a finally block to perform any cleanup operations when the exception is processed.
If an exception is thrown, the matching catch clause executes, and the control enters the finally block, if any.
The finalize-method name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory.
This method is called by the garbage collector to this object when it determines that the object is not referenced.
It is defined in the Object class, so all classes inherit it.
subclasses override the Finalize () method to organize system resources or perform other cleanup work.
The Finalize () method is called on the object before the object is deleted by the garbage collector.



2. What is the difference between String and StringBuffer?

String to manipulate a string of characters. Immutable classes.

StringBuffer is also an operation on a string of characters, but a mutable class.

String:
Is the object is not the original type.
As an immutable object, you cannot modify its value once it has been created.
The modification of a string object that already exists is to recreate a new object and then save the new value in.
A String is the final class, which cannot be inherited.

StringBuffer:

is a mutable object that does not re-establish the object as a string when modifying it
It can only be created by a constructor function,
StringBuffer sb = new StringBuffer ();
Note: You cannot pay a value by paying the value symbol.
SB = "Welcome to here!"; /error
After the object is created, the memory space is allocated in memory and a null is initially saved. to StringBuffer
The Append method can be used when paying the value.
Sb.append ("Hello");

StringBuffer is more efficient in string join operations than string:

String str = new String ("Welcome to");
str + = "Here";
The processing step is actually by building a stringbuffer, let Hou call append (), and finally
Then the StringBuffer tosting ();
In this case, the connection operation of string is more than stringbuffer some additional operations, of course, the efficiency of the compromise.

And because the string object is an immutable object, each operation sting a new object to hold the new value.
So the original object is useless, it will be garbage collection. This is also to affect performance.




The distinction of key keywords in 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.