자바 8
https://blog.naver.com/whydda/222227014403
은 이 글을 참고 ~
자바9
private 메서드
public interface Payment {
void reserve();
void certify();
void pay();
void status();
default void recodeStatusTime(){
startTime()
status()
endTiem()
}
private void startTime(){
System.out.println(System.currentTimeMillis())
}
private void endTime(){
System.out.println(System.currentTimeMillis())
}
}
try-with-resource + 실질적인 final 변수
BufferedReader br = Files.newBufferedReader(Paths.get("a.txt"));
try(br) {
String line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
final 변수이면 try매개 변수로 넣을 수 있다.
코드가 간결해질 수 있다.
콜렉션 팩토리 메서드
List<Integer> list = List.of(1,2,3);
Map<String, Integer> map = Map.of("k1","v1","k1","v1");
Set<Integer> set = Set.of(1,2,3)
of을 이용해 Collection을 생성할 수 있다.
자바 8에서는 Arrays.asList 등을 사용했어야 했는데 조금더 편해졌다.
Arrays (compare, mismatch)
int comp = Arrays.compare(a, b);
int firstIdx = Arrays.mismatch(a, b);
compare : 논리적인 순서를 체크 (같으면 0, a가 앞서면 음수, b가 앞서면 양수)
mismatch : 두 배열이 맞지 않는 첫 index를 리턴한다. 같으면 -1을 리턴한다.
자바 10
var
var a = 10;
var result = new ArrayList<Integer>();
a : int type으로 타입추론을 통해 적용된다.
result : ArrayList<Integer> 적용
자바 11
String
- isBlank() : 공백문자만 포함하고 있는지 확인
- Character.isWhitespace(int) 를 기준으로 확인
- lines()
- stream line 기준으로 스트림으로 쪼개진다.
- repeat()
- String str = "1".repeat(10) : 1을 10번 반복한다.
- strip(), stripLeading(), stripTrailing()
- Character.isWhirespace(int) 메소드를 기준으로 앞뒤 공백 제거 , trim 과는 조금 틀림 (U+0020) 이라 값 제거
Files
- Files.witeString() : 지정한 경로에 문자열을 파일로 저장 가능하다.
- Files.readString() : 지정한 경로에서 바로 문자열로 읽어 올 수 있다.
Files.writeString(Path.of("a.txt"), "string", StandardOpenOption.CREATE); String str = Files.readString(Path.of("a.txt"));
자바 12
- indent(int n) : n 만큼 들여쓰기
-
"abc\ndef".indent(3) -> abc\n def\n : 맨뒤 줄바꿈이 붙는다.
-
- <R> R transform(Function<? super String, ? extends R> f)
-
"20011231".transform(DataUtil::toLocalDate);
-
자바 14
switch 식
String grade = switch(mark) {
case 1 -> "일";
case 2 -> "이";
case 3 -> "삼";
default -> "백";
}
switch로 값을 만들 수 있게 한다.
변수에 할당이 가능
자바 15
텍스트 블록
String a = """
안녕
""";
String
- formatted()
-
String old = String.format("name: %s", "java"); //15이전 String new = "name: %s".formatted("java"); //15
-
NPE 메시지
System.out.println(name.getFirst().toUpperCase());
//메시지가 상세화 되었음
//예)
-----------------------------------------------------------------------------
java.lang.NullPointerException: Cannot invoke "String.toUpperCase()"
because the return value of "var15.Name.getFirst()" is null
-----------------------------------------------------------------------------
자바 16
Stream
- toList()
-
//전 List<Integer> nos2 = Stream.of(1,2,3).filter(n -> n % 2 == 0).collect(Collectors.toList(); //후 List<Integer> nos1 = Stream.of(1,2,3).filter(n -> n % 2 == 0).toList()
-
- mapMulti()
-
List<Integer> result = Stream.of(1,2,3) .mapMulti((Integer no, Consumer<Integer> consumer) -> { consumer.accept(no); consumer.accept(-no); }).toList();
- 값 하나로 여러개를 생성할 수 있다.
-
instanceof와 패턴 매칭
if (a instanceof String s) { // s: 패턴 변수
System.out.println(s)
}
if (a instanceof String t && t.isBlank()) { // s: 패턴 변수
System.out.println("blank")
}
if (!(a instanceof String b)) { // s: 패턴 변수
return;
}
System.out.println(b.toUpperCase()); //얼리리턴
record 클래스
record FullName(String first, String last) {
public String formatter() {
return first + " " + last;
}
}
FullName fn = new FullName("f", "l");
fn.first();
fn.last();
- first, last
- private final 필드
- 파라미터를 가진 생성자
- 같은 이름의 메서드(getter)
- hashCode, equals, toString
- 특징
- 기본 생성자 없음
- 값 변경 없음
출처 : 이 글은 최범균님의 유튜브 동영상을 토대로 작성하였습니다.
https://www.youtube.com/watch?v=7SlDdzVk6GE
'Back-End > Java' 카테고리의 다른 글
@Transactional 간단한 정리 (0) | 2021.08.18 |
---|