java enum class and common class 09_Math_UUID

1.Math class

java.lang.Math provides a range of methods for scientific computing, and are static.

  • Math class static properties:

Math.PI ratio of circumference of a circle to its diameter, i.e. pi.

[Example] calculated circumferential length

// 半径
double radius = 5.0;
// 计算圆周长
double circumference = 2*Math.PI*radius;
// 圆周长:3.141592653589793
System.out.println("园周长:" + circumference);
  • Static methods of the Math class:

Math.ceil (): returns the smallest integer greater than the parameter (ceiling rounding).

Math.floor (): returns the greatest integer less than the argument (floor rounding).

Math.round (): returns an integer rounding.

[Example] decimal rounding Case

// 地板取整
System.out.println(Math.floor(12.56)); // 输出:12.0
// 天花板取整
System.out.println(Math.ceil(12.56));  // 输出:13.0
// 四舍五入取整
System.out.println(Math.round(12.56)); // 输出:13

Math.max (data1, data2): returns the maximum of two numbers.

Math.min (data1, data2): returns the minimum of two numbers.

[Example] get the most value case of two numbers

// 获取两个数的最大值
System.out.println(Math.max(12.0, 5.0)); // 输出:12.0
// 获取两个数的最小值
System.out.println(Math.min(12.0, 5.0)); // 输出:5.0

Math.abs (data): absolute value.

Math.pow (a, b): a b-th power.

Math.sqrt (double a): square root.

[Example] abs, pow, and sqrt Case

// 取绝对值
System.out.println(Math.abs(-11.2)); // 输出:11.2
// a的b次方幂
System.out.println(Math.pow(2, 3));  // 输出:8.0
// 开平方根
System.out.println(Math.sqrt(9.0));  // 输出:3.0

Math.random (): returns 0.0 (inclusive) to 1.0 (exclusive) random number.

[Example] acquiring a random number between 1 and 10

// 获取区间为[1, 10]的随机整数(方式一)
double num1 = Math.ceil(Math.random() * 10);
// 获取区间为[1, 10]的随机整数(方式二)
int num2 = (int)(Math.random() * 10 + 1);

We can also be acquired by a random integer java.util.Random class provides member methods, e.g. rand.nextInt (10) is produced by [0, 1] is a random integer. .

[Example] acquiring a random number between 1 and 10

// 创建一个Random对象
java.util.Random random = new java.util.Random();
// 获取区间为[1, 10]的随机整数
int num = random.nextInt(10) + 1;

2.UUID class

Is the abbreviation UUID Universally Unique Identifier (Universally Unique Identifier), which is a standard software build, which purpose is to allow all of the elements in the distributed system, can have unique identification information by the central control terminal does not need to do identification of specific information. It is composed of a set of 32-digit hexadecimal numbers.

[Example] Java generated UUID way

import java.util.UUID;
public class Test {
	public static void main(String[] args) {
		// 生成一个随机的UUID对象
		UUID uuid = UUID.randomUUID();
		// 把UUID对象转化为字符串
		String code = uuid.toString();
		System.out.println(code);
	}
}

3. enumeration class

Enumeration (enum) type is JDK1.5 new features, it is a new type, allows constants to represent a specific piece of data, but also in the form of all types of security are represented.

Enumerated type definition includes enumeration enum declaration and body. Format is as follows:

enum 枚举名 {
    枚举体(常量列表)
}

Enumeration constants body is placed, a plurality of constants separated by commas.

[Example] to create enumerated types

enum Season {
    SPRING, SUMMER, AUTUMN, WINDER; 
}

Although the grammatical structure and syntax of the enum class is not the same, but after the compiler generates a class file, all enumerated types implicitly inherit from java.lang.Enum , in essence, is an enumeration class. The essence of each member of the enumeration is an example of the type of enumeration, they are by default public static final modification (can decompile view).

Simple to use [example] enumerated

// 枚举
enum Gender {
	MAN, WOMAN;
}
// Person类
class Person {
	// 姓名属性
	String name;
	// 性别属性:男或女。保证数据的安全性,选用枚举
	Gender sex;
	// 构造方法
	public Person(String name, Gender sex) {
		this.name = name;
		this.sex = sex;
	}
}
// 测试类
public class Test {
	public static void main(String[] args) {
		// 创建Person对象,并对属性赋值
		Person p = new Person("小明", Gender.MAN);
		// 输出:"name: 小明 sex: MAN"
		System.out.println("name: " + p.name + " sex: " + Gender.MAN);
	}
}

[Example] enumeration traversal

enum Week {
	星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期日;
}
// 测试类
public class Test {
	public static void main(String[] args) {
		// 遍历所有的枚举
		for(Week week : Week.values()) {
			System.out.println("week: " + week);
		}
	}
}

[Example] enumeration is used in the switch

enum Season {
    SPRING, SUMMER, AUTUMN, WINDER;
}
// 测试类
public class EnumDemo {
	public static void main(String[] args) {
		// 获取0到3之间的随机数
		int num = new java.util.Random().nextInt(4);
		System.out.println(num);
		// 根据随机数,得到枚举值
		Season season = Season.values()[num];
		switch(season) {
			// 在switch中Season.SPRING可以简写为SPRING
			case SPRING: 
				System.out.println("春天");
				break;
			case SUMMER: 
				System.out.println("夏天");
				break;
			case AUTUMN: 
				System.out.println("秋天");
				break;
			case WINDER: 
				System.out.println("冬天");
				break;
		}
	}
}

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 92 original articles · won praise 0 · Views 2589

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104784453