Practical Classes in Java

Posted by gunhoe86 on Sun, 05 May 2019 22:40:02 +0200

1. Enumerated is the use of

Enumeration (enum):

Reference data types: data, classes, interfaces, enumerations
switch supports int String enumeration (enum)
Enumeration uses encapsulation to solve unreasonable assignment problems
Write a demo test
First is an enumeration class of Gender gender, then I declare two constants

package com.xxx.demo;

public enum Gender {
	//Male, female
}

And then a student class, Student

package com.xxx.demo;

public class Student {
	public static void main(String[] args) {
		String name = "Li Xiaoyao";
		Gender gender = Gender.male;
		System.out.println("My name is:" + name + "\n My gender is:" + gender);
	}
}

This calls the constants in the Gender class, and you can only choose men or women.
Test Diagram:

2. Use of Packaging Classes

1. There is no function for the basic data type, so when you need to operate on the basic data type, you can use the corresponding packaging type.

2. Collection generics require data types to be wrapper types

The wrapper classes are shown in the java.lang package

Common API s for packaging types

1. Creation of Packaging Type
//Transfer a corresponding base data type value to the construct
Integer integer = new Integer(10);
//Transfer a value of type string to the construct, but the contents of the string must be convertible to the corresponding wrapper class
Integer integer2 = new Integer("10");
System.out.println(integer);
System.out.println(integer2);

2. Conversion of packaging type (converting basic data type values to packaging types directly using functions)
//Basic data type values can be converted to wrapper types through the static function valueOf
Integer valueOf = Integer.valueOf(10);
Character valueOf = Character.valueOf('h');

3. Packaging Type Conversion - > Basic Data Types

package com.xxx.demo;

public class Test3 {
	public static void main(String[] args) {
		Integer integer = Integer.valueOf(10);
		// Packaging types can be converted to their corresponding basic data types using xxValue()
		int intValue = integer.intValue();
		System.out.println(intValue);
	}

}

4.String->Basic Data Type/Packaging Type (In network data transmission, the data table layer is converted to a string, and the server accepts a string, but the string is not very convenient to manipulate some data, so it needs to be converted)

package com.xxx.demo;

public class Test4 {
	public static void main(String[] args) {
		// Declare a String string type with a value of 10
		String value = "10";
		// Strings can be converted to their corresponding basic data types
		int parseInt = Integer.parseInt(value);
		// Strings can be converted to their corresponding wrapper type
		Integer valueOf = Integer.valueOf(value);
	}

}

5. Basic/Packaging Type->String

package com.xxx.demo;

public class Test5 {
	public static void main(String[] args) {
		// Declare an integer a
		int a = 10;
		// Anything spliced with a + sign becomes a string
		String b = a + "Character string";
		// Convert basic data types to strings
		String string = Integer.toString(a);
		// Convert package type to string
		Integer c = Integer.valueOf(a);
		String string2 = c.toString();
	}
}

Unpacking

Unpacking: Convert packaging type to basic data type
Decoration: basic data type conversion to packaging type
After JDK5, Java supports automatic unboxing and packing

package com.xxx.xiang;

public class Demo {
	public static void main(String[] args) {
		// Boxing: Basic data type converted to packaging type
		Integer a = 10;
		// Unpacking: Convert packaging type to basic data type
		int b = a;
	}

}

Notes on the use of packaging classes and basic data types

1. The wrapper class is a reference data type so it can accept null values, while the basic data type cannot.
2. Packaging types are not used to replace basic data types, but to compensate for the inability of basic data types to use functions.
Basic data types can be used to compare value content with ==
Packaging type defaults to null, basic data type defaults to 0 false, etc.
3. Basic and packaging types can be confused in most cases when transferring data parameters

3.Math and Random classes

Math Math Classes

package com.xxx.math;

public class Demo {
	public static void main(String[] args) {
		// Find the minimum of both
		int min = Math.min(1, 2);
		System.out.println("The minimum value between these two numbers is:" + min);

		// Find the maximum of both
		int max = Math.max(10, 100);
		System.out.println("The maximum value between these two numbers is:" + max);

		// Evaluate Absolute Value
		int abs = Math.abs(-10);
		System.out.println("Absolute values are:" + abs);

		// Power operation 2 to the third power
		double pow = Math.pow(2, 3);
		System.out.println("2 The third power is:" + pow);

		// Rounding
		long round = Math.round(10.5);
		System.out.println("After rounding, the result is:" + round);

		// ceil
		double ceil = Math.ceil(10.2);
		System.out.println("After rounding up, the result is:" + ceil);

		// Rounding Down
		double floor = Math.floor(10.8);
		System.out.println("The result of rounding down is:" + floor);

		// Square
		double sqrt = Math.sqrt(4);
		System.out.println("The result after squaring is:" + sqrt);

		// Find random numbers to generate random numbers between 0 and 0.99999
		double random = Math.random();
		System.out.println(random);

		// Generate a random integer within a specified range [min,max)
		int fanwei = (int) (Math.random() * (max - min)) + min;
		System.out.println(fanwei);
	}

}

Topics: Java network