본문 바로가기

코테 공부/java

equals(), == 차이 equals() 는 메소드 (객체끼리 내용을 비교) == 은 비교를 위한 연산자. -주소값 비교와 내용 비교 비교할 수 있는 대상의 차이 equals 메소드는 비교하고자 하는 대상의 내용 자체를 비교하지만, == 연산자는 비교하고자 하는 대상의 주소값을 비교. CBV(Call By Value) 는 기본적으로 대상에 주소값을 가지지 않는 것으로 값을 할당받는 형태로 사용. 예를 들어 int, float, double, byte 등 primitive type. CBR(Call By Reference) 는 대상을 선언했을 때, 주소값이 부여. 그래서 어떠한 객체를 불러왔을 때는 그 주소값을 불러. Class, Object(객체) 해당. String a = "aaa111"; String b = a; String.. 더보기
Integer .valueOf(str) .parseInt(str) 차이 Integer.parseInt(String s) 원시 데이터인 int 타입을 반환 Integer.valueOf(String s) Integer 래퍼 객체를 반환 차이는 거의 없고 대부분의 기본 숫자 데이터타입 래퍼 클래스들인 Integer, Long, Double, Float 등과 같은 클래스 안에 포함되어 있을 거라고 한다. 더보기
str.substring() str.substring(int index) index 포함한 인덱스부터의 문자열 리턴 str.substring(int beginIndex, int endIndex) beginIndex 포함한 인덱스부터 endIndex 전(endIndex 포함 안한)까지 리턴 더보기
삼항연산자, System.out.print(여기 안에도 삼항연산자 가능) public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (n % 2 == 0) { System.out.printf("%d is even", n); } else System.out.printf("%d is odd", n); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.print(n + " is "); System.out.println(n%2 == 0 ? "even" : "odd"); } 이런 식으로도 가능 // 삼항 연산자 .. 더보기
대소문자 변환 Character.isUpperCase(인자), str.toUpperCase(), Character.toUpperCase(인자) Character.isUpperCase(인자) String 대문자로 변환 : toUpperCase() String str = "abc" str = str.toUpperCase(); //"ABC" String 소문자로 변환 : toLowerCase() String str = "ABC" str = str.toLowerCase(); //"abc" Char 의 경우는 아래 메서드를 사용한다. Char 대문자로 변환 : Character.toUpperCase(변환을 원하는 단어) Char c = "a" c = Character.toUpperCase(c); //"A" Char 소문자로 변환: Character.toLowerCase(변환을 원하는 단어) Char c = "A" c = Character.toLowerC.. 더보기
문자열 반복, x만큼 간격이 있는 n개의 숫자 String str int n n번 str 반복 str.repeat(n) x랑 i이 int 다보니 i*x + x 가 int 범위를 넘어가면 결과를 int로 못받고 계산이 잘못되는 결과가 나온거라고 한다. 아무리 봐도 로직은 맞는데 왜 테스트 두개가 통과가 안되나 답답해하다가 번뜩 타입 생각나서 다행히 시간 많이 안썼다. class Solution { public long[] solution(int x, int n) { long[] answer = new long[n]; for (int i = 0; i < n; i++) { answer[i] = (long)i*x + x; } return answer; } } 더보기
Scanner public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { System.out.print(sc.next()); } } 더보기
배열 초기화 //크기 할당 & 초기화 없이 배열 참조변수만 선언 int[] arr; int arr[]; // 선언과 동시에 배열 크기 할당 int[] arr = new int[3]; String[] arr = new String[3]; // 기존 배열의 참조 변수에 초기화 할당하기 int[] arr; arr = new int[5]; //5의 크기, 초기값 0으로 채워진 배열 // 선언과 동시에 배열의 크기 지정 및 값 초기화 int[] arr = {1,2,3,4,5}; int[] arr = new int[] {1,3,5,2,4}; int[] odds = {1,3,5,7,9}; String[] weeks = {"월","화","수","목","금","토","일"}; // 2차원 배열 선언 int[][] arr = new .. 더보기