프로그래밍/JAVA

개선전 코드 (코드 리뷰 및 개선점을 요청받은 코드)더보기import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collection;import java.util.List;import java.util.Optional;import kwan.utility.customException.CustomReflectionException;public class Utility { static Connection con; static PreparedStatement ps; s..
바이트 입출력 // 문자 동일(try catch)InputStream is = new FileInputStream("파일경로");OutputStream os = new FileOutputStream("파일경로");byte[] array = new byte[1024];while(true) { int num = is.read(data) if (num == -1) break; os.write(array, 0, num);}os.flush();os.close();is.close();  보조 스트림// InputStream To ReaderReader reader = new InputStreamReader(new FileInputStream("경로"));// OutputStream To WriterWri..
표현 및 기호설명[]한개의 문자[abc] // a,b,c 중 하나의 문자[^abc] // a,b,c 이외의 하나의 문자[a-zA-Z] // a~z, A-Z 중 하나의 문자\d한 개의 숫자, [0-9]와 동일\s공백\w한 개의 알파벳 또는 한 개의 숫자, [a-zA-Z_0-9]와 동일\...모든 문자 중 한 개의 문자?없음 또는 한개의 문자*없음 또는 한 개 이상+한 개 이상{n}정확히 n개{n,}최소한 n개{n,m}n개부터 m개까지a|ba 또는 b()그룹핑  String regExp = "(02|010)-\\d{3,4}-\\d{4}";String data = "010-123-4567";boolean result = Pattern.matches(regExp, data); // true
Kotlin에서의 var 개념 도입 switch문의 Null 처리switch(object) { case null -> {} // object가 null일 경우 case null, default -> {} // object가 null이거나 선택되지 않은 경우}  switch문의 Instance 확인String data = switch(obj) { case Integer i -> String.valueOf(i); case String s -> "\" + s + "\"; case null, default -> "unknow";};// 이런식으로 obj의 타입을 확인해서 case에 있는 해당 변수로 return할 수 있음  가상 스레드
외운다기 보다 이 흐름을 생각하면 더 좋을 것 같다Connection으로 부터 conn을 받아온다이때 DriverManager의 도움을 받아 사용하고자 하는 DB와 Connection을 생성한다Connection이 생겼으면 하고자 하는 동작에 대한 것이 필요해진다PreparedStatement를 준비하고 원하는 동작(SQL)문을 등록해준다PreparedStatement(pstmt)에 필요한 것들을 setXXX를 통해 세팅해준다이후 execute를 해주면 된다  public class Main4 { public static void main(String[] args) throws IOException { String SQL; PreparedStatement pstmt = nu..
클래스의 멤버로 선언된 인터페이스\해당 클래스와 긴밀한 관계를 맺는 구현 객체를 만들기 위함public class Button { public static interface ClickListener{ void OnClick(); } private ClickListener clickListener; public void setClickListener(ClickListener clickListener) { this.clickListener = clickListener; } public void click() { this.clickListener.onClick(); }}(main)Button btnOk = new Button();cla..
public class A { void method1(int arg) { // final int arg //로컬 변수 int var = 1; // final int var = 1 //로컬 클래스 class B { static int field = 1; // 정적 필드(Java 17) (B 생성자) static void method2() { sout(arg); // 읽기 가능 // arg = 1; // 불가능 } // 정적 메소드(Java 17) } // 로..
// sealed 키워드를 사용하면 permits 뒤에 상속 가능한 자식 클래스를 지정해야 함public sealed class Person permits Employee, Manager {}// sealed 처리된 Person 을 상속하는 Employee, Manager는// final 또는 non-sealed 키워드를 쓰거나 sealed 키워드를 사용해서 또 다른 봉인 클래스로 선언해야함public final class Employee extends Person {}public non-sealed class Manager extends Person{}
yield 키워드의 경우 Java13부터 사용 가능하고yield가 있으면 default가 만드시 존재해야 함int score = 0;switch(grade) { case 'A': case 'a': (print) break; case 'B': case 'b': int result = 100 - 20; score = result; break; case 'C': case 'c': (print) break; default: score = 60;}switch(grade) { case 'A', 'a' -> (print); case 'B', 'b' -> { int result = 100 - 20; ..
람다식@FunctionalInterfaceinterface MyLambdaFunction { int max(int a, int b);}public class Main { public static void main(String[] args) { // 람다식을 이용한 익명함수 MyLambdaFunction lambdaFunction = (int a, int b) -> a > b ? a : b; System.out.println(lambdaFunction.max(3, 5)); }}  람다식 메소드 참조(x, y) -> Math.max(x, y)(x, y) 중복 , 리턴 값이 또 다른 메서드 호출Math::max;@FunctionalInterfaceinter..
스트림 문법 반환타입메서드booleanallMatch(Predicate T> predicate)Returns whether all elements of this stream match the provided predicate.booleananyMatch(Predicate T> predicate)Returns whether any elements of this stream match the provided predicate.static  Stream.Builderbuilder()Returns a builder for a Stream. Rcollect(Collector T,A,R> collector)Performs a mutable reduction operation on the elements of thi..
! 클래스 다이어그램 Toolhttps://draw.iohttps://staruml.io/ Refactoring GURU // 디자인패턴 추천23가지 패턴 정리
류가든
'프로그래밍/JAVA' 카테고리의 글 목록