개발자 미니민의 개발스터디

4. 다양한 클래스 (캘린더, Math, Object, wrapper 클래스 등)

by mini_min

◾  캘린더 객체를 만들어서 시스템 날짜를 만들 수 있다. 

Calendar cal = Calendar.getInstance();
System.out.printf("%1$tF %1$tT\n", cal);

 

◾ Math 클래스로 수학적인 계산이 가능하다.

double a;
a = Math.PI;
System.out.println(a);
a = Math.sin(Math.PI * 30/180.0); // sin30 //라디안
System.out.println(a);

 

◾ cos 값 구하기

java.util.Scanner sc = new java.util.Scanner(System.in);
double a, b;
System.out.print("각도? ");
a = sc.nextDouble();
b = cos(PI * a / 180.0);
System.out.println(a + "각도의 cos 값 : " + b);
sc.close();

 

◾ Object 클래스의 toString() 메소드는 "클래스이름@해쉬코드" 를 문자열로 반환

class Test1 {
int a = 10;
int b = 10;
public void disp() {
System.out.println(a + "," + b);
}
}
Test1 t1 = new Test1();
Test1 t2 = new Test1();
System.out.println(t1.toString());
System.out.println(t1);
// System.out.print 메소드에서는 t1만 출력해도
// t1.toString() 를 출력한 것과 동일한 결과를 출력한다.

 

◾ object equals() 는 주소 비교 false 

◾ 해쉬코드 : JVM이 해당 객체를 빠르게 검색할 수 있도록 만들어진 값이다.

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", t1.hashCode()); //16진수

 

◾ wrapper 클래스 : 기본형 데이터를 객체 단위로 처리할 수 있도록 만들어진 클래스

기본형(int long double) 등은 null 가질 수 없지만, wrapper 클래스는 가질 수 있다.

내용불변(immutable)이다.

내용이 변경되면 새로운 영역의 메모리가 할당되고 기존 인스턴스는 garbage collector의 대상이 된다.

int a = 10, n;
Integer b = null;
//Integer : int에 대응하는 wrapper 클래스
//wrapper 클래스 객체는 null을 가질 수 있지만 int 변수는 null을 가질 수 없다.
b = a; //서로 대응되는 wrapper clas는 대입 가능
//intInteger 로 자동 변환(autoboxing)
System.out.println(a + "," + b); //10,10 but 둘이 같은건 아니다.
b = 50;
n = b; //Integerint 로 자동 변환(auto-unboxing)
System.out.println(n + "," + b);
System.out.println(b == n); //true
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; // 가능

 

◾ wrapper 클래스에 다양한 메소드로 처리 가능한 것들이 있음

System.out.println("int 최대값 : " + Integer.MAX_VALUE);
System.out.println("int 최대값 : " + Integer.MIN_VALUE);
System.out.println("SIZE : " + Integer.SIZE); //비트로 출력
System.out.println("BYTES : " + Integer.BYTES);
System.out.println("TYPE : " + Integer.TYPE);

 

◾ 문자열을 정수 변환도 가능하다.

숫자로 바꿀 수 없는 경우에는 문자로 반환 불가하다.

a = Integer.parseInt("1,234")  //런타임 오류(NumberFormatException)

//문자열을 정수로 변환
a = Integer.parseInt(s1);
b = Integer.parseInt(s2);
System.out.println(a + b); //579

 

◾ Integer.parseInt 에 2번째로 오는 인자로 2진수, 16진수 등으로 변경도 가능하다.

a = Integer.parseInt("b1", 16);
System.out.println(a); //177
a = Integer.parseInt("1100", 2);
System.out.println(a); //12 (2진수니까)
a = Integer.parseInt("1000");
System.out.println(a); //177

 

◾ 정수를 문자열로 변환도 가능하다. toString() 

2진수의 문자열로 변환도 가능하다.

//정수를 문자열로
a = 123;
s1 = Integer.toString(a); //정수가 to 스트링으로 변환
System.out.println(s1);
a = 652;
s1 = Integer.toBinaryString(a); //2진수의 문자열로 변환
System.out.println(s1);

 

◾ BigInteger 클래스 : 아주 큰 정수를 다루기 위한 클래스

//BigInteger : 아주 큰 정수를 다루기 위한 클래스
BigInteger a = new BigInteger("123456789123456789123456789123456789");
BigInteger b = new BigInteger("123456789123456789123456789123456789");
BigInteger c = a.add(b); //큰 수의 덧셈!
System.out.println(c);

 

◾ BigDecimal : 소수점 관련 클래스다.

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.89 에서 3456 값만 가져온것

 

 

 

 

 

블로그의 프로필 사진

블로그의 정보

개발자 미니민의 개발로그

mini_min

활동하기