(Development support library) Optional class

Optional class

The main function of the Optionnal class is to deal with null.

package 开发支持类库;

interface IMessage{
	public String getContent();
}

class MessageImpl implements IMessage{
	@Override
	public String getContent() {	//覆写接口方法
		return "www.baidu.com";		
	}
}

class MessageUtil{
	public static IMessage getMessage() {
		return new MessageImpl();	//返回一个实例化对象
	}
	public static void useMessage(IMessage msg) {
		if(msg != null) {
			System.out.println(msg.getContent());		//有可能因为出现null,而导致空指向
		}
	}
}

public class Optional类 {
	public static void main(String[] args) {
		MessageUtil.useMessage(MessageUtil.getMessage());	
	}
}

www.baidu.com

The party receiving the citation is often passive. So in order to solve this passive processing operation, there is an OPtionnal class in the Java class photo, which can implement null processing operations. The following operation methods are provided in this class:

  • Return empty data: public static<T>Optional<T>empty();
  • Get data: public T get();
  • Save the data, but null is not allowed: public static <T>Optional<T>of(T value);

        If there is a null when saving the data, a NullPointException will be thrown.

  • Save the data, allowing to be empty: public static <T> Optional<T>ofNullable(T value);
  • Return other data when empty: public T orElse(T other);

Example: Modify the program and complete it in accordance with the regular program

package 开发支持类库;

import java.util.Optional;

interface IMessage{
	public String getContent();
}

class MessageImpl implements IMessage{
	@Override
	public String getContent() {	//覆写接口方法
		return "www.baidu.com";		
	}
}

class MessageUtil{
	public static Optional<IMessage> getMessage() { 	//返回一个Optional对象
		return Optional.of(null);		//空值
	}
	public static void useMessage(IMessage msg) {
		if(msg != null) {
			System.out.println(msg.getContent());		//有可能因为出现null,而导致空指向
		}
	}
}

public class Optionnal类 {
	public static void main(String[] args) {
		IMessage temp = MessageUtil.getMessage().get();	//获取数据
		MessageUtil.useMessage(temp);	
	}
}

Exception in thread "main" java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:208)
    at java.base/java.util.Optional.of(Optional.java:111)
    at Development support class library.MessageUtil.getMessage(Optionnal
    class.java:18 ) at Development support class library.Optionnal class.main(Optionnal class.java:29)

At this time, the content of Optional.of() is null, and an error will occur when running and it will tell you where the method is. The null pointer is abnormal.

Since null content is allowed to be stored in the Optional class, null processing can also be performed when data is obtained. If you use the get() method, the "Exception in thread "main" java.lang.NullPointerException" exception will occur during execution, so you can replace it with orElse(T other);

Example: Use orElse(T other) to resolve null values

package 开发支持类库;

import java.util.Optional;

interface IMessage{
	public String getContent();
}

class MessageImpl implements IMessage{
	@Override
	public String getContent() {	//覆写接口方法
		return "www.baidu.com";		
	}
}

class MessageUtil{
	public static Optional<IMessage> getMessage() { 	//返回一个Optional对象
		return Optional.ofNullable(null);		//有对象
	}
	public static void useMessage(IMessage msg) {
		if(msg != null) {
			System.out.println(msg.getContent());		//有可能因为出现null,而导致空指向
		}
	}
}

public class Optionnal类 {
	public static void main(String[] args) {
		IMessage temp = MessageUtil.getMessage().orElse(new MessageImpl());	//如果数据为null,则返回一个Message对象
		MessageUtil.useMessage(temp);	
	}
}

www.baidu.com

In the handling of all reference data types, null is an important technical problem, so the Optional class provided by JDK1.8 is very helpful for null handling, and it is also a program class that will be used a lot in future project development.

Guess you like

Origin blog.csdn.net/weixin_46245201/article/details/112566713