Common Object ①② for Java

Source: Internet
Author: User
Tags dateformat time in milliseconds

Common Object ① for Java

Java has a lot of common objects, here only the Lang packet commonly used object,date (Calendar,dateformat,math), system,runtime to introduce.
㈠object is the root class of a class hierarchy. Each class uses object as a superclass. All objects, including arrays, implement methods of this class.
In object, equals (Object obj), Hashcode (), toString () are common to each class, GetClass () is used to reflect, and finalize () is a method related to garbage collection.

The remaining notify (), Notifyall (), wait (), wait (long timeout), wait (long timeout, int nanos) are thread-dependent.

Finally there is the Clone () method, which is to create and return a copy of this object.
The ㈡date class represents a specific moment, accurate to milliseconds. Many of the methods in date are obsolete, in fact, Java suggests that date be replaced by two classes: Starting with JDK1.1, you should use the Calendar class to convert between date and Time fields, and use the DateFormat class to format and parse date strings.
In other words, the operation of the date itself has been replaced by the calendar, and the date formatting, according to the original habit, you can continue to use DateFormat to deal with.
The calendar is an abstract class that should be instantiated by calling GetInstance () first. You can make more connections here. There is also the math () class.
㈢system is a class that is related to the local runtime environment, where a commonly used copy of the arraycopy () array, Currenttimemillis () returns the current time in milliseconds, and exit (int status) terminates the currently running Java Virtual machines and GC () methods that can invoke the garbage collector proactively.
㈣runtime: Each Java application has a runtime class instance that enables the application to connect to the environment in which it is running.
The current runtime can be obtained through the GetRuntime method. The application cannot create its own instance of the Runtime class.
Here you need to know the Exec () method and the GC () method. The GC () method is also an active call to run the garbage collector. In fact, the System.GC () method in this province is called this method.
It's just a simpler notation.
The collective source code is as follows:
public static void GC () {
Runtime.getruntime (). GC ();
}

Common Object ② for Java

String,stringbuffer,stringbuilder.
Here we discuss only the difference in efficiency of the three, rather than exploring its representation in memory. For memory analysis, refer to the "Java Memory and wrapper classes" section.
First, the difference between StringBuffer and StringBuilder is thread-safe. The relationship between the two is much like the difference between Hashtable and HashMap.
StringBuffer the same is the early time of the class, and StringBuilder in a relatively late time.
In situations where thread safety is guaranteed, it is more efficient to prioritize StringBuilder.
And the string class is very special. Only one situation is discussed here.
String str1 = "a" + "B" + "C";
StringBuffer str2 = new StringBuffer ("a"). Append ("B"). Append ("C");
Which is the higher efficiency? The answer is STR1.
The reason is to start with the compilation and operation of Java, and for constants, compile-time stores their literal values directly instead of their references.
That is to say, at compile time, the result of their connection is extracted into "abc", which is equivalent to string str1 = "ABCDE" in the class file.
Then it's not the case for variables. Like what:
String s2 = "a";
String s3 = "B";
String s4 = "C";
String str1 = s2 +S3 + s4;
As a result, the str2 is more efficient.
It can be said that the majority of multi-string operation StringBuffer is more efficient, this is the above example is indeed the exception.

Common Object ①② for 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.