언어
-
Java I/O <2 - InputStream>언어/자바 2023. 12. 23. 04:05
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. 글 작성에 참고한 영상입니다. Java IO - Output Streams by Kody Simpson InputStream 기본적으로 들어온 입력을 byte로 변환해주는 역할을 한다. System.in의 read 메소드를 사용해서 콘솔에서 받은 입력을 어떻게 보여주는지 확인해보자. 1. read() 입력된 스트림에서 1byte를 읽어서 int로 된 결과를 반환한다. - 영문 입력 (UTF-8 기준 1Byte 입력) class TestInputStream { public static void main(String[] args) { InputStream..
-
Java I/O <1 - OutputStream>언어/자바 2023. 12. 23. 03:46
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각하면 좋을 것 같습니다. 글 작성에 참고한 영상입니다. Java IO - Output Streams by Kody Simpson OutputStream 데이터를 Byte로 변경하여 목적지로 보내는 역할을 한다. 크게 1개의 Byte를 사용하는 것과 Byte[ ]을 사용하는 경우로 나누어서 볼 수 있겠다. - 단일 Byte 사용 1. byte타입 class TestOuputStream { public static void main(String[] args) { byte b = 75; OutputStream out = System.out; out.write(b); o..
-
너무 다양한 List를 만드는 방법들..언어/자바 2023. 12. 20. 01:27
자바스크립트에서 "[]"로 정적 배열 개념없이 동적 배열만 사용해온 나에게 자바는 너무 다양한 List를 생성하는 방법을 가진 것이 아닌가라는 생각이 든다. Q.그래서 배열이 필요하면 난 무엇을 사용해야되나? 우선 내 머릿속에 있는 List와 배열의 개념부터 명확하게 할 필요가 있다. 기존에 자바스크립트에 '배열'이란 단어로 퉁치고 있던 "동일한 타입의 목록"을 재정의하자. 1. new int[size] 작은 개념에서 부터 시작하자. 자바에서 배열은 정적 배열을 이야기한다. ( 배열 == 정적 배열 ) int[] arr = new int[3]; arr[0] = 0; arr[1] = 1; arr[2] = 2 arr[3] = 3; // error int[] arr2 = { 1, 2 }; 사이즈가 정해져있다...
-
Queue의 add와 offer의 차이언어/자바 2023. 12. 20. 01:15
add vs offer Queue는 요소의 추가를 위해 'add'와 'offer' 2가지 메소드를 제공한다.(공식문서) boolean offer(Object o) {} void add(Object o) throws Exception {} offer는 실패시 false를 반환하지만, add는 throw를 던진다. 비슷하게는 poll과 remove가 있다. 사용의 유연성을 위해 2가지의 메소드를 제공하는 것 같지만, 기본적으로 offer와 poll을 사용하는 편이 안정적일 것 같다. Exception: add, remove, element Safe: offer, poll, peek Deque는? 데크도 queue와 마찬가지로 add, remove, get을 사용하는 경우는 Exception을 던지고, off..
-
instanceof는 어떻게 작동하나?언어/자바 2023. 12. 20. 01:13
Runtime 기준 aObj instanceof Class / Interface 좌항의 인스턴스가 우항의 클래스 혹은 서브클래스의 인스턴스인지 확인한다. 런타임에 확인하기 때문에 실제 메모리의 인스턴스의 타입에 의해 결정된다. 인터페이스에 대해서도 작동한다. class Animal {} class Dog extends Animal {} class Cat extends Animal {} public class Main { public static void main(String[] args) { Animal animal = new Dog(); if (animal instanceof Dog) { System.out.println("It's a Dog!"); } else if (animal instanceof ..
-
동적바인딩과 @Override언어/자바 2023. 12. 20. 00:56
이번 글의 목차 코드 실행 순서를 맞춰보자 부모 클래스가 버젓이 print를 가지고 있는데도, 부모 클래스에서 자식의 print가 실행된다!? 런타임에는 무슨일이? 왜 이렇게 작동할까? super와 this를 이용한 오버로딩 메소드 호출 코드의 실행 순서를 맞춰보자 아래 코드가 어떻게 작동되는지 100% 확신이 있으시다면 뒤로 가셔도 좋습니다 😇😇 class Base { Base() { System.out.println("Base Class Constructor"); print(); } void print() { System.out.println("Base"); } } class Derived extends Base { int x; Derived(int x) { System.out.println("Der..
-
자바코드는 어떻게 실행되는가? <3>언어/자바 2023. 12. 18. 17:41
앞의 글에서 연결되는 글입니다. 실제로 JDK와 JRE는 어디에 어떤 형태로 있는지 확인해봅니다. JDK와 JRE는 어디에 어떤 형태로 있을까? 현재 m1 맥을 사용하고 있고, brew를 통해서 자바를 install해서 사용하고 있다. 아래에 루트에 가면 자바의 버전별로 jdk가 설치 되어있는 것을 확인 할 수 있다. (자바 8버전 jdk 폴더를 확인할 수 있다.) cd /Library/Java/JavaVirtualMachines 원하는 버전의 jdk 경로 속으로 들어가면 cd /Library/Java/JavaVirtualMachines/Contents/Home bin 폴더에서 javac 실행파일을 찾을 수 있다. 우리가 javac를 실행시키면 이 위치의 javac 를 실행시키는 것이다. 또 jre/li..
-
자바코드는 어떻게 실행되는가? <2>언어/자바 2023. 12. 18. 17:30
앞의 글에서 연결되는 글입니다. JDK와 JRE의 다른 점을 체감할 수 있는 예제를 살펴볼 것입니다. 결론 JDK를 설치하면 JRE + javac를 비롯한 개발에 필요한 많은 것들이 함께 설치되어 코드 작성과 컴파일 실행이 모두가능. JRE만 설치하면 실행환경만 설치되기 때문에 컴파일이나 다른 개발에 필요한 작업들을 할 수가 없다. JDK와 JRE를 어떻게 다른가? 이전 글에서 알아봤던 JDK와 JRE를 직접 설치해보고, 어디에 설치되는지? 어떤 모습인지 확인해보자. 다음과 같은 시나리오로 가지고 진행해보자. 1. 자바가 설치되어있지 않은 나의 컴퓨터에서 자바 코드를 작성한다. 2. 작성된 코드가 정상 실행되는지 확인한다. 3. 친구의 컴퓨터에도 자바가 설치되어있지 않다. 4. 자바로 작성된 코드를 친구..