언어
-
List.of와 NullPointExecption언어/자바 2024. 3. 27. 08:14
자바에서 List.of를 사용해서 간편하게 List를 잘 만들어서 사용하고 있습니다. 모든 요소가 null 인 경우를 찾기 위해서 아래와 같은 방법을 사용하려고 했는데 public boolean isEmpty() { List checkList = List.of(title, requirements, preferred, stack, icon, status); return checkList.stream().noneMatch(Objects::nonNull); } 바로 NullPointException이 발생합니다. 잉? 자바 문서를 찾아보니 Null을 넣을 수 없다고 떡하니 적혀있습니다. (공식 문서) 대안을 찾아보니 Arrays.asList를 사용할 수 있을 것 같습니다. public boolean isEmp..
-
List 정렬에 대하여언어/자바 2024. 1. 12. 01:06
List를 정렬하는 여러가지 방법들을 정리해본다. Collections.sort 에서부터 stream.sorted까지 살펴본다. 글의 흐름은 Head First Java를 기초로 하고 있다. 정렬하는 방법들 1. Collections.sort(List) List인 경우만 정렬이 가능하다. /* 배열은 정렬 불가 */ String[] sArr = {"z", "b", "k"}; Collections.sort(sArr); // error /* List로 변경해서 정렬 */ String[] sArr = {"z", "b", "k"}; List sList = Arrays.asList(sArr); System.out.println("b4" + sList); // z,b,k Collections.sort(sList);..
-
왜 타입스크립트는 환영받는가? (feat.타입과 에러)언어/자바스크립트 2023. 12. 25. 23:46
🎉 타입스크립트를 왜 써야하는가를 살펴보다가 JS의 에러에 관한 이야기로 흘러들어와버렸다.. 타입스크립트 문법을 배우기에 앞서, 자바스크립트의 수퍼셋이라고 하는데 무엇이 나아졌길래 사람들에게 사랑받는걸까? 찾아보니 가장 먼저 눈에 띄는 부분은 runtime error가 발생하지 않도록 개발단계에서 error를 처리할 수 있게 해준다는 거 같은데... 뭔가 확 와닿지 않았다. 런타임 에러? 왜 발생하면 안되는거야? 👀 프로그래밍을 하면서 에러는 나의 프로젝트의 진행을 방해하고, try-catch 구문으로 처리하는 것들이라고만 생각했지, 그 이상 생각해보지 않았던 것 같다.. 런타임이란 무엇인가 간단하게 생각하면, 프로그램이 실행되는 환경을 이야기한다. JS를 다루는 사람들에게 런타임 환경은 browser나..
-
Java IO <7 - Writer>언어/자바 2023. 12. 25. 23:30
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. 자바 I/O 관련 마지막 글입니다 Writer Reader 클래스와 비슷하게 OutputStream을 String기반으로 사용할 수 있도록 하는 클래스이다. Writer = OutputStream + String 핸들링 1.FileWriter FileWriter를 사용하면 기존에 String을 byte[]로 변경해서 write작업을 수행하던 것을 하지않고 곧 바로 String을 사용해서 write를 할 수 있다. FileOutputStream public static void main(String[] args) throws IOException { F..
-
Java I/O <6 - Reader>통계언어/자바 2023. 12. 25. 23:25
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. Reader byte를 다루는 InputStream과 달리 Reader 클래스는 String을 다룰 때 사용되는 클래스이다. Reader클래스들은 InputStream을 내부에서 사용하거나 랩핑하여 사용한다. Reader = InputStream + String 핸들링 1. FileReader 영문을 입력할 때와 한글을 읽을 때를 각각 살펴보자. // test.txt this is a test sentence 한글 테스트 입니다. public static void main(String[] args) throws IOException { FileInpu..
-
Java I/O <5 - Buffered Stream>언어/자바 2023. 12. 25. 23:19
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. Buffered I/O Stream 앞에서 다루었던 I/O Stream들을 보다 효율적으로 다루기 위한 방법이다. 버퍼를 사용할 때와 안할 때의 차이를 왜 효율적인지 생각해보자. 동기식 I/O의 동작 방식 핵심은 파일이나 네트워크를 다루는 I/O는 CPU연산에 비해 엄청나게 느리기 때문에 적게 할수록 효율적이다는 것이다. 위 그림을 보면 1. InputStream은 매번 read()가 호출될 때마다 운영체제 혹은 디스크 레벨까지 접근을 하게 된다. 하지만 2번 BufferedInputStream 같은 경우에는 일단 디스크에서 버퍼 크기만큼의 데이터를 ..
-
Java I/O <4 - Network Stream>언어/자바 2023. 12. 25. 23:16
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. 네트워크를 왜 I/O로 분류하는가를 이해해보자. Socket과 I/O Stream 쓰레드를 사용하지 않고, socket에서 제공하는 Input/Output Stream을 사용해서 간단하게 메세지를 console에서 확인해보자. 1. 클라이언트에서 서버로 메세지를 보내보자 우선 원하는 포트를 지정해서 응답을 기다리는 서버를 만든다. 서버에 도착한 stream을 console에 출력해보자. // server public static void main(String[] args) { ServerSocket serverSocket = new ServerSock..
-
Java I/O <3 - FileStream>언어/자바 2023. 12. 25. 23:10
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. FileOutputStream 앞에서 다루었던 System클래스 속의 stream들은 콘솔을 기반으로 I/O를 수행했다면, 이번에 다루는 FileStream들은 파일을 기반으로 I/O를 수행한다. 1. 파일 쓰기 - 덮어쓰기 FileOutputStream 클래스에 전달된 경로에 파일이 없는 경우, 파일이 생성되고, 있는 경우는 해당 경로의 파일을 이용한다. public static void main(String[] args) throws IOException { FileOutputStream fileOutputStream = new FileOutput..