방법
- Long.parseLong()
- new Long(str).longValue()
- Long.valueOf(str).longValue()
예제
String str = "30";
Long strLong;
//str이 유효한 숫자가 아니면 NumberFormatException 발생
//방법1 : Long.parseLong(str)
strLong = Long.parseLong(str);
//방법2 : new Long(str).longValue()
strLong = new Long(str).longValue();
//방법3 : Long.valueOf().longValue()
strLong = Long.valueOf(str).longValue();
주의점
- 입력된 String 값이 유효한 숫자가 아니면 NumberFormatException 발생
- String 값이 숫자인 게 확실하거나 예외를 정상 처리하고 싶지 않은 경우, try-catch 처리 생략 가능
출처 : https://www.delftstack.com/ko/howto/java/how-to-convert-a-string-to-long-in-java/
'Backend > Java' 카테고리의 다른 글
[자바의 신] 17장 (0) | 2022.07.25 |
---|---|
[자바의 신] 16장 (0) | 2022.07.25 |
[Java] 정수 최대값, 최소값 구하기 (0) | 2022.03.28 |
[Java] int와 Integer 비교 (0) | 2022.03.21 |
[Error] java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener (0) | 2022.02.28 |
댓글