코딩테스트-알고리즘/문법

오늘의 자바 문법

닉네임생각즁 2024. 1. 6. 09:29

 

BigInteger
import java.math.BigInteger;

BigInteger bigNumber = new BigInteger("숫자");

 

- BigInteger가 문자열로 되어있기 떄문에 초기화할때 문자열을 인자값으로 넘겨줘야함

 

BigInteger 계산
num1.add(num2); // 덧셈
num1.subtract(num2); // 뺼셈
num1.multiply(num2); // 곱셈
num1.divide(num2); // 나눗셈
num1.remainder(num2); // 나머지

* 두 수 비교 : num1.compareTo(num2);

 

BigInteger 형 변환
num.intValue(); //BigIntger -> int
num.longValue(); //BigIntger -> long
num.floatValue(); //BigIntger -> float
num.doubleValue(); //BigIntger -> double
num.toString(); //BigIntger -> String

 

 

 

String -> int
Integer.parseInt(String s)

- int 반환

 Integer.valueOf(String s).intValue();

- valueOf가 Integer Object를 리턴하기 때문에 intValue()를 붙여 int로 반환시켜줌

- int n = Integer.valueOf(s) -> 사실 이렇게 해도 자동형변환되긴함

 

int -> String
Integer.toString(int n)
String.valueOf(int n)
 int n + "" 

'코딩테스트-알고리즘 > 문법' 카테고리의 다른 글

[오늘의 자바 문법] Map - getOrDefault  (0) 2024.01.12
오늘의 자바 문법  (0) 2024.01.07
오늘의 자바 문법 (ArrayList -> Array)  (0) 2024.01.05
오늘의 자바 문법  (0) 2023.12.25
오늘의 자바 문법  (1) 2023.12.23