Enumerating enum+Runtime runtime classes

Posted by MiniMonty on Thu, 11 Nov 2021 03:08:04 +0100

1, Enumeration class

The number of objects in a class is considered to be limited and fixed. You can list the objects in some classes one by one.

1. Enumerate class interface enum through enum description
2. Public enum day {Monday, Tuesday} can be directly enumerated in the enumeration class
3. Directly access the attribute name (enumeration object) through the enumeration class
4. Many methods are found by enumerating object calls
Each enum enumeration type defined by itself inherits java.lang.enum Object by default
There are two private attributes name enumeration object names ordinal order (index in enumeration class)
name() ordinal() method for obtaining relevant attribute information
Day enum=Day.valueOf("name");
Day[] days=Day.values();
monday.compareTo(tuesday);
5. You can define your own general attributes and methods in enum
The enumeration object must be enumerated on the first line of the enumeration class description, ending with a semicolon
Add self defined private properties
Provide private constructor (with parameters). We can't call constructor ourselves
6. The enum defined by itself cannot inherit other classes (single inheritance)
You can implement multiple interfaces and add specific methods
7. It can be used in the switch statement

  • 1. Try how to manually design (static constant singleton mode) Day (class as a description of 7 objects) if there is no enumeration type
    private construction method
    public static final
package myenum;

public class MyDay{//Describe 7 weeks
	//Similar to the idea of singleton mode
	
	//Construction method private
	private MyDay(){}
	//All objects are properties
	public static  final MyDay monday=new  MyDay();
	public static  final MyDay tuesday=new  MyDay();
	public static  final MyDay wednesday=new  MyDay();
	public static  final MyDay thursday=new  MyDay();
	public static  final MyDay firday=new  MyDay();
	public static  final MyDay saturday=new  MyDay();
	public static  final MyDay sunday=new  MyDay();

	//General attribute general method random design
	
}
package myenum;

public class Test{
	public static void main(String[] args){
		MyDay day=MyDay.monday;//One object and seven objects were accessed in the day class
	}
}
  • 2. After JDK1.5, enum types can be defined directly
    The enum type defined by ourselves directly inherits enum (java.lang package) by default
    Our own defined enum type can no longer write extensions, but it can be implemented

Enum type
There are two properties
Name --- > name of enumeration object name() gets the name attribute
ordinal - > enumeration objects are listed in the class in a similar order to index, which starts from 0. ordinal() gets the sequence number
Some common methods
valueOf(); Get the corresponding enumeration object through the given name.
values(); Get all enumeration objects ----- > return an array Day []
compareTo(); You can compare two enumeration objects. int negative number comes first and positive number comes last
toString(); Because this method has no final modification, it can be overwritten (overridden)

  • Each enum type defined by ourselves will inherit enum by default and Object indirectly.
package myenum;
public enum Day{
	//Describes the objects of the seven current classes
	monday,tuesday,wednesday,thursday,friday,saturday,sunday

}
package myenum;
public class Test{
	public static void main(String[] args){
		Day day=Day.monday;
		//Day --- > objects of enumeration type inherit the Object class by default
		//In addition to the methods inherited from the Object class, there are also some methods to prove that the enum type we create inherits enum by default
		//Each Enum type defined by ourselves will inherit Enum by default and indirectly inherit Object
		
		Day d=Day.valueOf("monday");//Call the static method and find an object by name called monday
		System.out.println(d.name()+"------"+d.ordinal());
		
		Day[] days=Day.values();//Get all enumeration objects
		for(Day d:days){
			System.out.println(d.name()+"------"+d.ordinal());
		}
	}
}
  • 3. Application of switch internal judgment enumeration
//Enter a string monday and output the corresponding information
Scannner input=new Scanner(System.in);
System.out.println("Please enter an English word for a week");
String key=input.nextLine();
Day day=Day.valueOf(key);//The corresponding enumeration object is found through the entered English word
switch(day){
	case monday:
		System.out.println("You entered Monday");
		break;
	case tuesday:
		System.out.println("You entered Tuesday");
		break;
	case wednesday:
		System.out.println("You entered Wednesday");
		break;
	case thursday:
		System.out.println("You entered Thursday");
		break;
	case friday:
		System.out.println("You entered Friday");
		break;
	case saturday:
		System.out.println("You entered Saturday");
		break;
	case sunday:
		System.out.println("You entered week 7");
		break;
	default:
		System.out.println("An error occurred");
}
  • 4. We can also describe some of our properties or methods in enum.
    You must describe some enumerations in the first line of the enum class, and finally end with a semicolon;
    You can define your own properties
    Class helps us create objects of enumerated types
    The enumeration type needs to be provided with a corresponding constructor. The constructor can only be overloaded with private decoration
package myenum;

public enum Day{
	//Describes the objects of the seven current classes
	monday("Monday",1),tuesday("Tuesday",2),wednesday,thursday,friday,saturday,sunday;
	private String name;
	private int index;
	private Day(){}
	private Day(String name,int index){
		this.name=name;
		this.index=index;
	}
	public Stirng getName(){
		return this.name;
	}
	public void setName(String name){
		this.name=name;
	}
}
Day x=Day.monday;
x.getName();
x.setName("ha-ha");

2, Runtime class

Memory management issues
Stack memory - variable space method execution temporary execution space is reclaimed immediately after execution is completed
Heap memory ---- we apply for new object space ourselves. Garbage collector GC object space without any reference points is regarded as garbage
Method area ---- there is only one static member of constant class template, which is not recycled
System.gc();// Without any reference, the code can be written back, and it is returned to the garbage collector. It is not recovered by GC has the final say.
In order to see the effect of object recycling:
1. Override the finalize method in the class to inherit from Object
2.Runtime class singleton mode Runtime.getRuntime(); Get object
long=maxMemory maximum space totalMemory space used in the space developed freeMemory space not used in the space developed
OutOfMemoryError heap memory overflow error heap space
StackOverflowEoor stack memory overflow error

  • Several memory management methods are provided in the Runtime class:
    maxMemory
    totalMemory
    freeMemory
    Heap memory overflow error OutOfMemoryError: java heap space
  • There is a finalize method in the Object class. If overridden, Object recycling can also be seen
  • GC system provides a thread recycling algorithm
package gc;
public class Test{
	public static void main(String[] args){
		Person p=new Person();
		try{
			Thread.sleep(2000);
		}catch(Exception e){

		}
		p=null;
		System.gc();
	}

}
package gc;
public class Person{
	//The Person class inherits Object by default
	//hashCode  equals   toString getClass  wait  notify  notifyAll  clone
	//A protected method in finalize object class
	Public Person(){
		System.out.println("person The object has been created");
	}
	public void finalize(){
		System.out.println("Person The object has been recycled");
	}
}

//1. No abstract class or interface 2. No parameterless constructor 3. Constructor private (singleton)

  • In test class main
Runtime r=Runtime.getRuntime();
long max=r.maxMemory();
long total =r.totalMemory();
long free=r.freeMemory();
System.out.println(max);
System.out.println(total);
System.out.println(free);
System.out.prinrln(----------------------------);
byte[] b=new byte[10000000];
long max2=r.maxMemory();
long total2 =r.totalMemory();
long free2=r.freeMemory();
System.out.println(max2);
System.out.println(total2);
System.out.println(free2);

Enter as follows:

  • In the test class main, if the available space is not enough, the available space will be expanded and the value of toal will be increased.
Runtime r=Runtime.getRuntime();
long max=r.maxMemory();
long total =r.totalMemory();
long free=r.freeMemory();
System.out.println(max);
System.out.println(total);
System.out.println(free);
System.out.prinrln(----------------------------);
byte[] b=new byte[(int)(max1/2)];
long max2=r.maxMemory();
long total2 =r.totalMemory();
long free2=r.freeMemory();
System.out.println(max2);
System.out.println(total2);
System.out.println(free2);

  • If the space creation is full (one array occupies all the space new byte[max1] or two arrays occupy half the space respectively), an exception will be thrown.

Topics: Runtime