[JAVA] 클래스들

Ming·2022년 10월 2일
0

자바

목록 보기
10/14

Math 클래스

수학관련 static 메서드들의 집합이다.

Math클래스의 메서드

메서드설명예제결과
static double abe(double s)
static float abs(float f)
static int abs(int f)
static long abs(long f)
주어진 값의 절댓값을 반환int i = Math.abs(-10);
double d = Math.abs(-10.0);
i=10
d=10.0
static double ceil(double a)주어진 값을 올림하여 반환double d = Math.ceil(10.1);
double d2 = Math.ceil(-10.1);
double d3 = Math.ceil(10.000015);
d=11.0
d2=-10.0
d3=11.0
static double floor(double a)주어진 값을 버림하여 반환double d = Math.floor(10.8);
double d2 = Math.floor(-10.8);
d=10.0
d2=11.0
static double max(double a, double b)
static float max(float a, float b)
staic int max(int a, int b)
static long max(long a, long b)
주어진 값을 비교하여 큰 값을 반환double d = Math.max(9.5, 9.50001);
int i = Math.max(0, -1);
d=9.50001
i=0
static double min(double a, double b)
static float min(float a, float b)
staic int min(int a, int b)
static long min(long a, long b)
주어진 두 값을 비교하여 작은 값을 반환double d = Math.max(9.5, 9.50001)
int i = Math.max(0, -1)
d=9.5
i=-1
static double random()0.0~1.0범위의 임의의 double 값을
반환(1.0은 범위에 포함 X)
double d = Math.random();
int i=(int)Math.random()*10 +1
0.0≤d<1.0
1≤i<11
static double rint(double a)주어진 double 값과 가장 가까운 정수값을
double형으로 반환.
단, 두 정수의 정가운데 있는 값은 짝수를 반환
double d = Math.rint(1.2)
double d2 = Math.rint(2.6);
double d3 = Math.rint(3.5);
double d4 = Math.rint(4.5);
d = 1.0
d2 = 3.0
d3 = 4.0
d4 = 4.0
static long round(double a)
static long round(float a)
소수점 첫째자리에서 반올림한 정수값(long)을 반환.
두 정수의 정가운데 있는 값은 항상 큰 정수를 반환
long l = Math.round(1.2);
long l2 = Math.round(2.6);
long l3 = Math.round(3.5);
long l4 = Math.round(4.5);
l = 1
l2 = 3
l3 = 4
l4 = 5

round() 예제

💡 소수점 아래 세 번째 자리에서 반올림하기
  1. 원래 값에 100을 곱한다.

  2. 위의 결과에 Math.round()를 사용한다.

  3. 위의 결과를 다시 100.0으로 나눈다.

    ❗️ 100으로 나누면 int형으로 바뀜

double d = 90.7552
double d2 = Math.round(d*100)/100.0; //소수점 셋째자리에서 반올림

// d=90.7552
// d2=90.76

Wrapper 클래스

8개의 기본형(객체 X)을 객체로 다뤄야할 때 사용하는 클래스이다.

자료형래퍼클래스생성자활용
booleanBooleanBoolean(boolean value)
Boolean(String s)Boolean b = new Boolean(true);
Boolean b2 = new Boolean(”true”);
charCharacterCharacter(char value)Character c = new Character(’a’);
byteByteByte(byte value)
Byte(String s)Byte b = new Byte(10);
Byte b2 = new Byte(”10”);
intIntegerInteger(int value)
Integer(String s)Integer i = new Integer(100);
Integer i2 = new Integer(”100”);
shortShortShort(short value)
Short(String s)Short s = new Short(10);
Short s2 = new Short(”10”);
longLongLong(int value)
Long(String s)Long l = new Long(100);
Long l2 = new Long(”100”);
floatFloatFloat(double value)

Float(float value)
float(string s) | Float f = new Float(1.0);
Float f2 = new float(1.0f);
Float f3 = new Float(”1.0f”); |
| double | Double | Double(double value)
Double(String s) | Double d = new Double(1.0);
Double d2 = new Double(”1.0”); |

[Chapter 10] Calendar 클래스


Calendar 클래스

추상클래스이므로 객체를 생성할 수 없고 getInstance()를 통해 구현된 객체를 얻어야한다.

Calendar cal = Calendar.getInstance();
Calendar cal = Calendar.getInstance(); //현재 날짜와 시간으로 생성
int thisYear = cal.get(Calendar.YEAR); //올해가 몇년인지
int lastDayOfMonth = cal.getActualMaximum(Calendar.DATE); //이 달의 마지막날

Calendar에 정의된 필드

필드명설명
YEAR
MONTH월(0부터 시작)
WEEK_OF_YEAR그 해의 몇 번째 주
WEEK_OF_MONTH그 달의 몇 번째 주
DATE
DAY_OF_MONTH그 달의 몇 번째 일
DAY_OF_YEAR그 해의 몇 번째 일
DAY_OF_WEEK요일
DAY_OF_WEEK_IN_MONTH그 달의 몇 번째 요일
HOUR시간(0~11)
HOUR_OF_DAY시간(0~23)
MINUTE
SECOND
MILLISECOND천분의 일초
ZONE_OFFSETGMT 기준 시차(천분의 일초 단위
AM_PM오전/오후

0개의 댓글