1、JDK10之局部变量类型推断var
Java是一种强类型, 许多流行的编程语言都已经支持局部变量类型推断,如js,Python,C++等JDK10 可以使用var作为局部变量类型推断标识符
Local-Variable Type Inference(局部变量类型推断),顾名思义只能用做为局部变量
注意:
1.仅适用于局部变量,如 增强for循环的索引,传统for循环局部变量
2.不能使用于方法形参、构造函数形参、方法返回类型或任何其他类型的变量声明
3.标识符var不是关键字,而是一个保留类型名称,而且不支持类或接口叫var,也不符合命名规范
//根据推断为 字符串类型
var strVar = "springboot";
System.out.println(strVar instanceof String);
//根据10L 推断long 类型
var longVar = 10L;
//根据 true推断 boolean 类型
var flag = true;
var flag = Boolean.valueOf("true");
System.out.println(flag instanceof Boolean);
// 推断 ArrayList<String>
var listVar = new ArrayList<String>();
System.out.println(listVar instanceof ArrayList);
// 推断 Stream<String>
var streamVar = Stream.of("aa", "bb", "cc");
System.out.println(streamVar instanceof Stream);
if(flag){
System.out.println("这个是 flag 变量,值为true");
}
for (var i = 0; i < 10; i++) {
System.out.println(i);
}
try (var input = new FileInputStream("validation.txt")) {
}
2、JDK11之新增HttpClient客户端
这个功能在JDK 9中引入并在JDK 10中得到了更新,最终JDK11正式发布,支持 HTT/1.1, HTTP/2
官方文档 http://openjdk.java.net/jeps/321
常用类和接口:
HttpClient.Builder -------- HttpClient 构建工具类
HttpRequest.Builder---------HttpRequest 构建工具类
HttpRequest.BodyPublisher---------将java 对象转换为可发送的HTTP request body字节流, 如form表单提交
HttpResponse.BodyHandler-----------处理接收到的 Response Body
创建HttpClient
//var httpClient = HttpClient.newBuilder().build();
var httpClient = HttpClient.newHttpClient();
提交get请求
private static final String targetUrl = "http://api.xdclass.net/pub/api/v1/web/all_category";
//创建请求路径
private static final URI uri = URI.create(targetUrl);
//GET请求
private static void testGet() {
//var httpClient = HttpClient.newHttpClient();
//设置建立连接超时 connect timeout
var httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofMillis(5000)).build();
//设置读取数据超时 read timeout //key:参数名 v:值
var request = HttpRequest.newBuilder()
.timeout(Duration.ofMillis(3000))
.header("key1", "v1")
.header("key2", "v2")
.uri(uri).build();
try {
//处理接收到的结果
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
提交Post请求
private static final String targetUrl ="https://api.xdclass.net/pub/api/v1/web/web_login";
private static final URI uri = URI.create(targetUrl);
//POST请求
private static void testPost() {
var httpClient = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(uri)
//json格式则使用下面数据
//.header("Content-Type", "application/json")
//.POST(HttpRequest.BodyPublishers.ofString("{\"phone\":\"13113777337\",\"pwd\":\"1234567890\"}"))
//form表单则使用下面配置
.header("Content-Type", "application/x-www-formurlencoded")
.POST(HttpRequest.BodyPublishers.ofString("phone=13113777337&pwd=1234567890"))
.build();
try {
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
提交异步GET请求
//异步GET请求
private static final String targetUrl = "http://api.xdclass.net/pub/api/v1/web/all_category";
private static final URI uri = URI.create(targetUrl);
//异步请求通过CompletableFuture实现。
private static void testAsynGet(){
var httpClient = HttpClient.newBuilder().build();
var request = HttpRequest.newBuilder().timeout(Duration.ofMillis(3000))
.header("key1", "v1")
.header("key2", "v2")
.uri(uri).build();
try {
CompletableFuture<String> result = httpClient
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(result.get());
} catch (Exception e) {
e.printStackTrace();
}
}
提交http2请求
HTTP2协议的强制要求https,如果目标URI是HTTP的,则无法法使用HTTP 2协议
private static final String targetUrl = "https://http2.akamai.com/demo";
private static final URI uri = URI.create(targetUrl);
private static void testHttp2() {
var httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(3000))
.version(HttpClient.Version.HTTP_2)
.build();
var request = HttpRequest.newBuilder()
.timeout(Duration.ofMillis(3000))
.header("key1", "v1")
.header("key2", "v2")
.uri(uri)
.build();
try {
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
System.out.println(response.version());
} catch (Exception e) {
e.printStackTrace();
}
}
3、jdk11类新增repeat
public class Main {
public static void main(String[] args) throws Exception {
String text = "xdclass.net";
//String类新增repeat(int num) 方法,用于字符串循环输出
System.out.println(text.repeat(2));
}
}