首页 >> 知识 >> 从lombok的val和var到JDK的var关键字

从lombok的val和var到JDK的var关键字

从lombok的@val和@var到JDK的var关键字 前言一、Lombok中的val和var1.1 lombok.val1.2 lombok.var二、Java中的var关键字2.1 如何使用?2.2 为什么推出var关键字?三、该如何抉择呢? Reference

前言

近期因项目中的开源框架版本升级导致项目的整体jdk版本被迫从万年的java8升级到了java11,于是草莓视频在线观看APP也从该开源框架中看到了对于我来说一个比较陌生的身影—val,于是便开始一小波的学习。

一、Lombok中的val和var 1.1 lombok.val

首先看官方文档介绍:

You can use val as the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final. This feature works on local variables and on foreach loops only, not on fields. The initializer expression is required.

val is actually a ‘type’ of sorts, and exists as a real class in the lombok package. You must import it for val to work (or use lombok.val as the type). The existence of this type on a local variable declaration triggers both the adding of the final keyword as well as copying the type of the initializing expression which overwrites the ‘fake’ val type.

WARNING: This feature does not currently work in NetBeans.

大致的意思就是val是用于

网站地图