Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- interrupted()
- this와 this() 차이
- 2차원배열 구간합
- Java
- 상속과 참조
- @AllArgsConstructor
- 슈더코드
- map()
- 백준 1235번
- 합배열
- ajax
- 구간합구하기
- SQL
- MariaDB Query Log
- 마리아DB 쿼리 로그
- 백준
- function test
- 구간합
- 백준 11660번
- 생성자
- json
- 백준 11659번
- pseudo-code
- jquery
- 자바 람다식
- select
- @NoArgsConstructor
- this
- Bean LifecCycle
- InterruptException
Archives
- Today
- Total
평범한 연구소
[JAVA] 기본 API | Object, Wrapper class 관련 함수 본문
java.lang.Object
- equlas()
- - 주소 비교
- - 변수.toString(비교할값)
- toString()
- - '클래스이름@해쉬코드' 를 문자열로 반환
- - 변수.toString()
- hashCode()
Object 메소드 예제
package ch07.unit2;
public class Ex01 {
public static void main(String[] args) {
Test1 t1 = new Test1();
Test1 t2 = new Test1();
/*
- Object의 toString() 메소드: "클래스이름@해쉬코드" 를 문자열로 반환
*/
System.out.println(t1.toString());
System.out.println(t1);
// System.out.print() 메소드에서는 t1만 출력해도
// t1.toString()를 출력한 것과 동일한 결과 출력.
System.out.println();
System.out.println(t1 == t2); // 객체는 주소 비교. false
System.out.println(t1.equals(t2));
// Object의 equals()는 주소 비교. false
// 해쉬코드: JVM이 해당 객체를 빠르게 검색할 수 있도록 만들어진 값
System.out.println(t1.hashCode()); // 10진수
System.out.printf("%x \n",t1.hashCode()); // 16진수
System.out.printf("%h \n",t1.hashCode()); // 16진수
}
}
class Test1 {
int a = 10;
int b = 10;
public void disp() {
System.out.println(a+","+b);
}
}
래퍼클래스 (Wrapper class)
- Byte, Short, Integer, Long ... Boolean.
- 기본 자료형과의 차이점
- - 래퍼클래스는 null 가질 수 있다.
- - 래퍼클래스는 자료형끼리 비교할 수 없다. Integer값을 Long에 대입 불가. 상하관계 성립 X. 형변환도X
- 박싱 Boxing: 기본타입→래퍼클래스 (autoboxing: int가 Integer로 자동 변환)
- 언박싱 Unboxing: 래퍼클래스→기본 (auto-unboxingInteger가 int로 자동 변환)
- Integer.parseInt(): 문자열→정수로 변환. 숫자로 바꾸지 못하는 것은 변환 못함. (ex.콤마,점,공백 등.)
- Double.parseDouble(): 문자열→double형으로 변환
- Integer.toBinaryString(): 2진수의 문자열로 변환
java.math.BigInteger
- BigInteger(): 21억이 넘는 아주 큰 정수 다루는 클래스.
- a.movePointLeft(n): 소수점을 왼쪽으로 n칸 이동
- divide(a, n, RoundingMode.DOWN): 소수점 이하 n자리 반올림
Wrapper class 예제
package ch07.unit3;
public class Ex02 {
public static void main(String[] args) {
int a = 10;
Integer b = 10;
System.out.println(a+", "+b);
long x = 100;
// Long y = 100; // 컴파일오류.
Long y = 100L;
System.out.println(x+", "+y);
// int < long은 성립되지만 Integer < Long는 성립되지 않음
x = a; // 가능
// y = b; // 컴파일오류. 크기 성립되지 않으므로
// y = (Long)b; // 컴파일오류. 형변환도 불가
/*
Integer n = 10;
Integer m = 10;
System.out.println(n==m); // true
*/
// 사용하지 말 것을 권장
Integer n = new Integer(10);
Integer m = new Integer(10);
}
}
package ch07.unit3;
public class Ex03 {
public static void main(String[] args) {
System.out.println("int 최대 값: " + Integer.MAX_VALUE);
System.out.println("int 최소 값: " + Integer.MIN_VALUE);
System.out.println("SIZE: " + Integer.SIZE+"bits");
System.out.println("BYTES: " + Integer.BYTES+"byte");
System.out.println("TYPE: " + Integer.TYPE);
String s1, s2;
int a,b;
s1 = "123";
s2 = "456";
System.out.println(s1+s2); // 문자열 결합. 123456
// 문자열을 정수로 변환
a = Integer.parseInt(s1);
b = Integer.parseInt(s2);
System.out.println(a+b); // 579
// a = Integer.parseInt("1,234"); // 런타임 오류(NumberFormatException)
// a = Integer.parseInt("1.234"); // 런타임 오류(NumberFormatException)
// a = Integer.parseInt("123 "); // 런타임 오류(NumberFormatException)
// a = Integer.parseInt("b1"); // 런타임 오류(NumberFormatException)
a = Integer.parseInt("b1", 16); // b1을 16진수로 변환
System.out.println(a);
String s = "123";
long a = Long.parseLong(s); // 문자열을 long형으로
System.out.println(a);
s = "123.56";
double b = Double.parseDouble(s);
System.out.println(b);
}
}
BigInteger 예제
package ch07.unit3;
import java.math.BigInteger;
public class Ex05 {
public static void main(String[] args) {
// BigInteger: 아주 큰 정수를 다루기 위한 클래스
BigInteger a = new BigInteger("123456789123456789123456789");
BigInteger b = new BigInteger("123456789123456789123456789");
BigInteger c = a.add(b);
System.out.println(c);
c = a.subtract(b);
System.out.println(c);
c = a.multiply(b);
System.out.println(c);
c = a.divide(b);
System.out.println(c);
c = a.pow(1000);
System.out.println(c);
}
}
package ch07.unit3;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Ex06 {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("123456789.123456789");
BigDecimal b = a.movePointLeft(3); // 소수점을 왼쪽으로 3칸 이동
BigDecimal c = new BigDecimal("3456.89");
System.out.println(a);
System.out.println(b);
System.out.println(c);
// BigDecimal d = a.divide(c); // 런타임오류
BigDecimal d = a.divide(c, RoundingMode.DOWN); // 반올림
System.out.println(d);
d = a.divide(c, 3, RoundingMode.DOWN); // 소수점 이하 3자리 반올림
System.out.println(d);
long x = c.longValue();
System.out.println(x); // 3456
}
}
'JAVA > 기본 개념' 카테고리의 다른 글
[JAVA] 정규식 (0) | 2022.07.21 |
---|---|
[JAVA] 재귀 호출, 오버로딩 (0) | 2022.07.20 |
[JAVA] 생성자, 접근제어자 (0) | 2022.07.20 |
[JAVA] 메소드 (0) | 2022.07.19 |
[JAVA] 클래스와 객체 (0) | 2022.07.18 |