전체 글
-
너무 다양한 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 ..
-
HashSet! 무엇을 기준으로 같다고 할 것인가?책/Head First Java 2023. 12. 20. 01:09
HashSet은 2번의 검증을 한다. HashSet은 이름 처럼 hash로 구성된 set이라고 보면 된다. 기본적으로 hash function이 반환하는 키를 기반으로 중복을 검사한다. 하지만 hash function은 같은 값을 반환할 수도 있기 때문에 혹은 임의로 hash key가 수정된 경우 때문에, 키가 같은 경우에는 obj.equals를 이용해서 한번 더 같음을 검사한다. 따라서 HashSet을 이용해서 객체의 중복을 거르고 싶다면, hashCode 메소드와 equals 메소드를 override 해야한다. class Movie { String title; @Override public int hashCode() { return title.hashCode(); } @Override public b..
-
원시타입과 래퍼클래스책/Head First Java 2023. 12. 20. 01:03
Integer vs int 이 둘은 무슨차이? 자바 1.5에서 auto-boxing과 auto-unboxing을 제공하여, 타입 측면에서는 차이가 없이 사용할 수 있다. (컴파일 타임에 확인 가능) int int는 Primitives 타입으로 4바이트만 차지한다. 메모리와 연산 속도에 이점이 있다. Integer 타입에 할당되는 경우, 컴파일 타임에 자동으로 Integer.valueOf()를 통해 auto-boxing 된다. Integer Integer는 Reference 타입이기 때문에 보다 많은 메모리를 차지한다. (기본 int 값 + 각종 메소드) 힙에 할당된다. null 사용 가능하다. int 타입에 할당되는 경우, 컴파일 타임에 자동으로 Integer.initValue()를 통해 auto-unb..
-
동적바인딩과 @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..
-
Homebrew 사용해서 새로운 자바 버전 추가설치 중 발생한 이슈기타/MAC OS 2023. 12. 18. 17:59
기존에 설치 되어있던 자바 관련 파일들을 지우고 JAVA8과 JAVA11을 설치하려던 중에 만난 에러에 대한 기록입니다. $ brew search jdk 위 명령어를 실행하니 바로 나오는 에러.. 뭔가 동일한 파일이 여러 경로에 설치된 상태인 것 같습니다. $ brew tap 위 명령어로 현재 사용중인 tap을 볼 수 있습니다. 아래 명령어로 adoptopenjdk/openjdk 탭을 제거하였습니다. cd /opt/homebrew/Library/Taps/ 에 있는 파일이 삭제됩니다. $ brew untap adoptopenjdk/openjdk 이제 원하는 버전을 설치하면 됩니다. $ brew install --cask 설치 성공!
-
자바코드는 어떻게 실행되는가? <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..