● 수학계산에 유용한 메소드를 가지고 있는 클래스
● Math 클래스는 생성자가 접근 불가하므로 객체로 만둘 수 없다.
● 모든 메소드는 static으로 사용해야 한다.
● 모든 메소드는 Math.xxx() 클래스 메소드 호출 형식으로만 사용한다.
static int abs(int f)
==> int, double, float, long 모든 타입이 있다.
매개변수 f 값의 절대 값을 반환
static double ceil(double f)
매개변수 f 값을 올림하여 반환
static double floor(double f)
매개변수 f 값을 버림하여 반환
static long round(double f)
매개변수 f 값을 반올림하여 반환
statuc int max(int a, int b)
매개변수 a, b 값을 비교하여 큰 쪽을 반환
static int min(int a, int b)
매개변수 a, b 값을 비교하여 작은 쪽을 반환
static long random()
0.0 ~ 1.0 범위의 임의의 double 값을 반환
→ 단, 1.0은 범위에 포함되지 않는다.
public class MathExam {
public static void main(String[] args) {
System.out.println("abs 절대값 ========");
System.out.println(Math.abs(10));
System.out.println(Math.abs(-10));
System.out.println(Math.abs(-10.1));
System.out.println("ceil 올림 ========");
System.out.println(Math.ceil(10.1));
System.out.println(Math.ceil(10.7));
System.out.println(Math.ceil(10.456));
// 소수 2째자리에서 올림하는 방법은?
System.out.println(Math.ceil(10.456 * 100) / 100.0); // 소수점 2째자리
System.out.println(Math.ceil(10.45645 * 1000) / 1000.0); // 소수점 3째자리
System.out.println("floor 버림 =======");
System.out.println(Math.floor(10.1));
System.out.println(Math.floor(10.7));
System.out.println(Math.floor(10.456));
// 소수 2째자리에서 버림하는 방법은?
System.out.println(Math.floor(10.456 * 100) / 100.0);
System.out.println("round 반올림 ======");
System.out.println(Math.round(10.1));
System.out.println(Math.round(10.5));
System.out.println(Math.round(10.456));
// 소수 2째자리에서 반올림하는 방법은?
System.out.println(Math.round(10.456 * 100) / 100.0);
System.out.println("max 비교후 큰 쪽 반환 ======");
System.out.println(Math.max(10, 20));
System.out.println(Math.max(10.1, 10.2));
System.out.println("min 비교후 큰 쪽 반환 ======");
System.out.println(Math.min(10, 20));
System.out.println(Math.min(10.1, 10.2));
System.out.println("random 랜덤한 값을 반환 ======");
System.out.println(Math.random());
System.out.println((int)Math.floor(Math.random() * 10));
System.out.println(random(10));
}
public static int random(int value) {
return (int)Math.floor(Math.random() * value);
}
}
● 입력 장치(키보드)로부터 데이터를 읽어 각 타입으로 값을 리턴하는 클래스
● 기본적으로 문자열로 반환하지만 정수, 실수, 논리 타입의 메소드도 있다.
● 입력되는 키 값을 공백으로 구분되는 토큰(" ",\t, \n) 단위로 읽는다.
● 키보드와 연결된 자바의 표준 입력 스트림이다.
● 입력되는 키를 바이트로 리턴하는 구식 스트림이다.
● 직접 사용하여 입력 처리를 할 경우 문자나 숫자로 변환하는 어려움이 있다.
● String next()
다음 토큰을 문자열로 리턴한다.
● byte nextByte()
다음 토큰을 바이트로 리턴한다.
● int nextInt()
다음 토큰을 정수로 리턴한다.
● double nextDouble()
다음 토큰을 문자열로 리턴한다.
● String nextLine()
‘\n’을 포함하는 한 라인을 일고 ‘\n’을 버린 나머지만 리턴한다.
● void close()
Scanner의 사용을 종료한다(메모리 정리).
● 날짜와 시간을 다루기 위한 static 클래스이다.
● 현재 날짜와 시간정보를 저장한 Calendar 인스턴스를 반환하는 메소드
Calendar cal = Calendar().getInstance();
● int get(int field)
현재 객체의 주어진 값의 필드에 해당하는 상수 값을 반환한다.
이 상수 값은 Calendar 클래스의 상수중에 있다.
● Date getTime()
현재의 객체를 Date 객체로 반환한다.
● long getTimeInMills()
현재 시간을 1/1000초 단위로 변경하여 반환한다.
● void set(int filed, int value)
현재 객체의 특정 필드를 다른 값으로 설정한다.
● void set(int year, int month, int date)
현재 객체의 년, 월, 일 값을 다른 값으로 설정한다.
● void set(int year, int month, int date, int hour, int minute, int second)
현재 객체의 년, 월, 일, 시, 분, 초 값을 다른 값으로 설정한다.
● void setTime(Date date)
date 객체의 날짜와 시간 정보를 현재 객체로 생성한다.
● void setTime(long mills)
현재 객체를 1/1000초 단위의 주어진 매개변수 시간으로 설정한다.
● void after(Object when)
when과 비교하여 현재 날짜 이후이면 true, 아니면 false를 반환한다.
● void before(Object when)
when과 비교하여 현재 날짜 이전이면 true, 아니면 false를 반환한다.
static int YEAR : 현재 년도
static int MONTH : 현재 월(단 1월은 0부터 시작)
static int DATE : 현재 월의 날짜
static int DAY_OF_YEAR : 현재 년도의 몇번째 날짜
static int DAY_OF_WEEK : 현재 요일(일요일:1, 토요일:7)
static int HOUR : 현재시간
static int MINUTE : 현재 분
static int SECOND : 현재 초
import java.util.Calendar;
public class CalendarExam {
public static void main(String[] args) {
// 주요 상수는 실제 내부에서는 그냥 정수이다.
// System.out.println(Calendar.YEAR);
// System.out.println(Calendar.MONDAY);
// Calendar 객체 가져오기
Calendar now = Calendar.getInstance();
System.out.println(now.get(Calendar.YEAR));
System.out.println(now.get(Calendar.MONTH) + 1);
System.out.println(now.get(Calendar.DATE));
// 시분초
System.out.println(now.get(Calendar.HOUR));
System.out.println(now.get(Calendar.MINUTE));
System.out.println(now.get(Calendar.SECOND));
// 요일 및 오전 오후를 출력
System.out.println(now.get(Calendar.DAY_OF_WEEK));
System.out.println(now.get(Calendar.AM_PM));
switch (now.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY: // 1
System.out.println("일");
break;
case Calendar.MONDAY: // 2
System.out.println("월");
break;
case Calendar.TUESDAY: // 3
System.out.println("화");
break;
case Calendar.WEDNESDAY: // 4
System.out.println("수");
break;
case Calendar.THURSDAY: // 5
System.out.println("목");
break;
case Calendar.FRIDAY: // 6
System.out.println("금");
break;
case Calendar.SATURDAY: // 7
System.out.println("토");
break;
default:
break;
}
switch (now.get(Calendar.AM_PM)) {
case Calendar.AM: // 1
System.out.println("오전");
break;
case Calendar.PM: // 1
System.out.println("오후");
break;
default:
break;
}
// // 현재 년월일 시분초를 찍는 방법
// int year = now.get(Calendar.YEAR);
// int month = now.get(Calendar.MONTH) + 1;
// int day = now.get(Calendar.DATE);
// int hour = now.get(Calendar.HOUR);
// int min = now.get(Calendar.MINUTE);
// int sec = now.get(Calendar.SECOND);
//
// System.out.println(year + "/" + month + "/" + day + " " + hour + ":" + min + ":" + sec);
System.out.println(now("-"));
// System.out.println(now.getTime());
}
public static String now(String symbol) {
Calendar now = Calendar.getInstance();
// 현재 년월일 시분초를 찍는 방법
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DATE);
int hour = now.get(Calendar.HOUR);
int min = now.get(Calendar.MINUTE);
int sec = now.get(Calendar.SECOND);
return year + symbol + month + symbol + day + " " + hour + ":" + min + ":" + sec;
}
}
● 현재의 날짜와 시간을 저장한 객체를 생성하는 클래스
● 객체화하여 사용가능하면 주로 현재 시간을 얻어올 때 많이 사용한다.
Date now = new Date();
System.out.println(now);
→ 출력 : Thu Mar 31 16:23:22 KST 2022
// 현재 시스템 시간을 영미권 형식으로 표시한다.
● long getTime()
1970년 1월 1일 0시 0분 0초로부터의 시간을 1/1000초 단위로 반환한다.
System.out.println(now.getTime());
→ 출력 : 1648711504544
● void setTime(long time)
time의 시간을 1970년 1월 1일 0시 0분 0초로부터 시간을
1/1000초 단위로 설정한다.
● getYear()
// 1900년도를 기준으로 한다.
이렇게 출력을 하면 현재 년도가 나오지 않는다.
현재 년도를 출력을 하려면 + 1900을 해줘야 한다.
System.out.println(now.getYear() + 1900);
→ 출력 : 2022
● boolean after(Date when)
when의 날짜가 현재 날짜 이후면 true, 아니면 false를 반환한다.
● boolean before(Date when)
when의 날짜가 현재 날짜 이전면 true, 아니면 false를 반환한다.
● int compareTo(Date anotherDate)
다른 날짜 객체와 비교하여 음수, 양수, 0의 결과를 반환한다.
● boolean equals(Object obj)
날짜의 값을 비교하여 그 결과를 반환한다.
● Date를 얻어온 시간을 사용하기(표현하기) 편리하도록 포맷을 설정하여 표시한다.
●printf와 같이 포맷 지정 날짜시간 표시 클래스라고 이해하면 된다.
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(sdf.format(now));
→ 출력 : 22. 3. 31. 오후 4:32
sdf = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");
System.out.println(sdf.format(now));
→ 출력 : 2022년 03월 31일 04시 34분 33초
→ 년, 월, 일, 시, 분, 초에 yyyy/MM/dd hh:mm:ss 이런식으로 할 수 있다.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExam {
public static void main(String[] args) {
Date now = new Date();
// 현재 시스템 시간을 영미권 형식으로 표시한다.
System.out.println(now);
// System.out.println(now.getTime()); //1646049115
// System.out.println(now.getTime()); //1576800000
// System.out.println(now.getYear() + 1900); // 1900년도를 기준으로 한다.
// System.out.println(now.getMonth() + 1);
// System.out.println(now.getDate());
// SimpleDateFormat 출력
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(sdf.format(now));
sdf = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");
System.out.println(sdf.format(now));
sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
System.out.println(sdf.format(now));
sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss.s");
System.out.println(sdf.format(now));
}
}
● 기본 자료형을 포장해서 형변환 또는 사용이 편리하게 만들어주는 참조형 클래스
기본형 타입의 값을 객체로 다루어야 할 때
기본형 타입이 아닌 객체로 값을 저장해야 할 때
매개변수로 객체를 넘길 때(특히 Object o)
객체간의 비교가 필요할 때
● Integer
정수형 int에 대한 wrapper 클래스
● Double
실수형 double에 대한 wrapper 클래스
● Character
문자형 char에 대한 wrapper 클래스
● Boolean
논리형 boolean에 대한 wrapper 클래스
● Byte
정수형 byte에 대한 wrapper 클래스
● Short
정수형 short에 대한 wrapper 클래스
● Long
정수형 long에 대한 wrapper 클래스
● Float
실수형 float에 대한 wrapper 클래스
● 기본 자료형을 Wrapper 클래스 자료형으로 변환하는 것을 말한다.
→ Integer ir = Integer.valueof(10);
● 박싱의 반대말로 Wrapper 클래스 자료형을 기본 자료형으로 변환하는 것을 말한다.
→ Int ia = ir.intValue();
● 박싱과 언박싱을 자바 컴파일러가 자동으로 알아서 처리해주는 것을 말한다.
public class WrapperExam {
public static void main(String[] args) {
// Boxing
Integer a1 = new Integer(10);
Integer a2 = new Integer(100);
// version 9 부터 추천 방식
Integer a3 = Integer.valueOf(10);
Integer a4 = Integer.valueOf(100);
Integer a5 = Integer.valueOf("100");
// int aa = 100; 4byte
// String ab = "100";
// System.out.println(aa == ab);
System.out.println(a3);
System.out.println(a4);
System.out.println(a4 == a5);
System.out.println(a4 + a5);
// 만약 문자열에 문자가 포함된다면
// System.out.println(Integer.valueOf("123a")); // 오류
// Unboxing
int ab3 = a3.intValue();
String ab4 = a3.toString();
System.out.println(ab3);
System.out.println(ab4);
// 실수형 wrapper
Double d1 = new Double(10.1);
Double d2 = Double.valueOf(10.1);
Double d3 = Double.valueOf("10.2");
System.out.println(d2);
System.out.println(d3);
int ad1 = d2.intValue();
double ad2 = d2.doubleValue();
System.out.println(ad1);
System.out.println(ad2);
// 오토 박싱 & 언박싱
// Integer a3 = Integer.valueOf(10); // 명시적 박싱
// String a = "123";
Integer au3 = 10; // Integer a5 = Integer.valueOf("100"); // 명시적 문자열 박싱
// Integer au4 = "100"; // 문자열로 오토 박싱 불가
int au5 = au3; // 오토 언박싱
int au6 = au3 + 100;
System.out.println(au5);
System.out.println(au6);
}
}