일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- oracle
- 자바
- 파이썬
- flet
- write by chatGPT
- 유닉스
- 자바암호
- NIO
- 자바네트워크
- 소프트웨어공학
- Database
- spring integration
- Java
- python
- chatGPT's answer
- GPT-4's answer
- spring data jpa
- 인프라
- 웹 크롤링
- write by GPT-4
- 신재생에너지 발전설비 기사
- jpa
- JVM
- kotlin
- 역학
- 고전역학
- 코틀린
- 시스템
- 데이터베이스
- 리눅스
- Today
- Total
기억을 지배하는 기록
Lombok features - val & var 본문
val
Finally! Hassle-free final local variables.
val was introduced in lombok 0.10.
Overview
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.
With Lombok
import java.util.ArrayList; import java.util.HashMap; import lombok.val; public class ValExample { public String example() { val example = new ArrayList<String>(); example.add("Hello, World!"); val foo = example.get(0); return foo.toLowerCase(); } public void example2() { val map = new HashMap<Integer, String>(); map.put(0, "zero"); map.put(5, "five"); for (val entry : map.entrySet()) { System.out.printf("%d: %s\n", entry.getKey(), entry.getValue()); } } } |
Vanilla Java
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class ValExample { public String example() { final ArrayList<String> example = new ArrayList<String>(); example.add("Hello, World!"); final String foo = example.get(0); return foo.toLowerCase(); } public void example2() { final HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(0, "zero"); map.put(5, "five"); for (final Map.Entry<Integer, String> entry : map.entrySet()) { System.out.printf("%d: %s\n", entry.getKey(), entry.getValue()); } } } |
var
Mutably! Hassle-free local variables.
- var was promoted to the main package in lombok 1.16.20; given that JEP 286 establishes expectations, and lombok's take on var follows these, we've decided to promote var eventhough the feature remains controversial.
- var was introduced in lombok 1.16.12 as experimental feature.
Overview
var works exactly like val, except the local variable is not marked as final.
The type is still entirely derived from the mandatory initializer expression, and any further assignments, while now legal (because the variable is no longer final), aren't looked at to determine the appropriate type.
For example, var x = "Hello"; x = Color.RED; does not work; the type of x will be inferred to be java.lang.String and thus, the x = Color.RED assignment will fail. If the type of x was inferred to be java.lang.Object this code would have compiled, but that's not howvar works.
'Library' 카테고리의 다른 글
Lombok features - @EqualsAndHashCode (0) | 2020.12.21 |
---|---|
Lombok features - @ToString (0) | 2020.12.21 |
Lombok features - @Getter and @Setter (0) | 2020.12.21 |
Lombok features - @Cleanup (0) | 2020.12.09 |
Lombok features - @NonNull (0) | 2020.12.09 |