1. JDK10之局部变量类型推断var讲解
- Java是⼀种强类型, 许多流⾏的编程语⾔都已经⽀持局部变量类型推断,如js,Python,C++等
- JDK10 可以使⽤var作为局部变量类型推断标识符
- Local-Variable Type Inference(局部变量类型推断),顾名思义只能⽤做为局部变量
- 注意仅适⽤于局部变量,如 增强for循环的索引,传统for循环局部变量
- 不能使⽤于⽅法形参、构造函数形参、⽅法返回类型或任何其他类型的变量声明
- 标识符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新增Http客户端
这个功能在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.Builder
创建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
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=1234567
890"))
.build();
try {
var response = httpClient.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
- 使⽤HttpClient提交异步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);
var result = httpClient.sendAsync(request,
HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(result.get());
} catch (Exception e) {
e.printStackTrace();
}
}
- http2.0
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();
}
}
JDK11之javac和java命令优化
- jdk11前运⾏java程序
- 编译
javac xxx.java - 运⾏
java xxx - jdk11后运⾏java程序(本地不会⽣成class⽂件)
java xxxx.java