In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about Java selection sorting and garbage collection mechanism, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
I. garbage collection mechanism
Creating an object will occupy memory, and if the program can no longer use an object during execution, the object is memory-consuming garbage. As a programmer, you don't have to worry about recycling garbage objects, because the java virtual machine automatically reclaims the memory space occupied by garbage objects.
When an object becomes garbage, it will temporarily remain in memory. If the garbage heap is full, the Java virtual machine has a garbage collection mechanism. The memory space occupied by the collected garbage objects will be released to the garbage collector. However, the program will have a lot of storage space. You can also have the java virtual machine garbage collected by calling the System.gc () method. When an object is released in memory, it can be called automatically through the finalize () method.
There are three kinds of objects in memory state:
Reachable state: when an object is created, more than one reference variable points to it, and the object is in the reachable state.
Recoverable state: no reference variables point to this object. Before the virtual machine performs garbage collection, the system calls all recoverable state objects finalize () to clean up. If the system redirects a reference variable to the object when calling the finalize () method, it will become reachable again; otherwise, the object will go into an unreachable state.
Unreachable state: when an object is disassociated with all reference variables and the system has called the finalize () method of all objects and does not make the object reachable, then the object will permanently lose the reference and eventually form an unreachable state.
Garbage collection process of java virtual machine
For example, the following code:
Class Person {/ / defines that the finalize method is called public void finalize () {System.out.println ("this object will be used as garbage collection...") before garbage collection;}} public class p9 {/ * * @ param args * / public static void main (String [] args) {/ / TODO Auto-generated method stub / / create two Person objects Person p1=new Person (); Person p2=new Person () / / set the object to null p1garbage null; p2garbage null; / / call the garbage collection method System.gc ();}
The result of the output is:
This object will be used as garbage collection.
This object will be used as garbage collection.
Second, Arrays class
Java provides the Arrays class for easy manipulation of arrays.
Arrays has the following features:
Array assignment: used for array filling through the Arrays.fill () method
Array sorting: sorts all the elements of the array by the Arrays.sort () method, in order from smallest to largest
Array comparison: determine whether the array element values are equal by the Arrays.equals () method
Find array elements: the Arrays.binarySearch () method looks for the specified element in the already sorted array by dichotomy and returns the subscript of the element
Array conversion string: the Arrays.toString () method converts the array to a string and outputs
Arrays class example
The code is as follows:
Public static void main (String [] args) {/ / TODO Auto-generated method stub int [] A1 = new int [] {5, 2, 3, 9}; int [] a2 = new int [] {5, 2, 3, 9}; / / Arrays.equals () method to determine whether array elements are equal System.out.println ("whether A1 array and a2 array are equal:" + Arrays.equals (A1, A2)); int [] b = Arrays.copyOf (A1, 6) System.out.println ("whether the A1 array and b array are equal:" + Arrays.equals (A1, b)); / / toString () method converts the array into the string System.out.println ("elements of the b array are:" + Arrays.toString (b)); / / Array.fill () method array assigns Arrays.fill (b, 2, 4, 1) System.out.println ("b array elements are:" + Arrays.toString (b)); / / Arrsays.sort () method array sort Arrays.sort (b); System.out.println ("b array elements are:" + Arrays.toString (b));}
The result of the output is:
Whether A1 array and a2 array are equal: true
Whether array A1 and array b are equal: false
The elements of array b are: [5, 2, 3, 9, 0, 0]
The elements of the b array are: [5, 2, 1, 1, 0, 0]
The elements of array b are: [0,0,1,1,2,5]
Third, the method of selective sorting
First find the subscript (index) of the location of the smallest element and swap this element with the first element.
Case study of selective sorting method
Public static void main (String [] args) {/ / TODO Auto-generated method stub int arr [] = {12 int 31, 25, 7, 38}; / / define an array of for (int iMago)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.