2024.03.04
StringBuffer
String은 불변객체이다. String을 암시적 생성할 때 같은 문자열이라면 새로운 객체가 생성되는 것이 아닌 기존 객체를 참조하여 만들어지는 이유이다.
String a = “불변”;
a = “불변객체”;
a = “불변객체이다.”;
이처럼 여러 번 수정해도 “불변”, “불변객체” 라는 String 객체들이 직접 수정되어 “불변객체이다.” 로 변경되는 것이 아니라 매번 새로운 객체들이 생성되어 메모리에 저장되는 것이다. 따라서 String을 여러 번 수정하게 되면 메모리 낭비가 심해진다. 이를 방지하기 위해 StringBuffer를 자바에서는 제공해준다.
public class StringBufferMain {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("여름 덥다!!");
System.out.println("1: " + sb);
//지정한 인덱스에 문자 삽입
sb.insert(2, '이');
System.out.println("2: " + sb);
//문자열 뒤에 문자열을 추가
sb.append("가을은 ");
System.out.println("3: " + sb);
sb.append("시원하다");
System.out.println("4: " + sb);
//시작 인덱스부터 끝 인덱스 전까지 문자열 대체
sb.replace(0,3,"여행가자");
System.out.println("5: " + sb);
//지정한 인덱스의 문자를 삭제
sb.deleteCharAt(0);
System.out.println("6: " + sb);
//시작 인덱스부터 끝 인덱스 전까지 문자열 삭제
sb.delete(0, 4);
System.out.println("7: " + sb);
//StringBuffer -> String 변환
String str = sb.toString();
System.out.println("str = " + str);
}
}
Math
public class MathMain {
public static void main(String[] args) {
int a = Math.abs(-10);
System.out.println("절대값 : " + a);
double b = Math.ceil(3.3);
System.out.println("올림 : " + b);
double c = Math.floor(3.7);
System.out.println("버림 : " + c);
int d = Math.round(3.5f);
System.out.println("반올림 : " + d);
int e = Math.max(3, 5);
System.out.println("최대값 : " + e);
int f = Math.min(4, 7);
System.out.println("최소값 : " + f);
}
}
Random
import java.util.Random;
public class MathMain02 {
public static void main(String[] args) {
String[] gift = {"스마트폰","TV","냉장고","꽝!"};
// 0 1 2 3
//난수 발생 : 0~1미만의 실수
double ran = Math.random();
System.out.println("발생한 난수 : " + ran);
int index = (int)(ran*4); // 0~3
System.out.println("정수로 변환한 난수 : " + index);
System.out.println("오늘의 선물 : " + gift[index]);
System.out.println("-----------------------");
String[] luck = {"귀인을 만남", "해외여행", "로또 당첨","피곤함"};
Random r1 = new Random();
//0부터 인자로 전달된 값의 전까지의 범위로 난수 발생
index = r1.nextInt(4);
System.out.println("발생한 난수 : "+index);
System.out.println("오늘의 운세 : "+luck[index]);
}
}
Wrapper
자동 Boxing
- 래퍼 객체를 써야 할 자리에 프리미티브 값을 썼을 때 일어나는 래퍼 객체로의 자동 변환
자동 Unboxing
- 프리미티브 값을 써야 할 자리에 래퍼 객체를 썼을 때 일어나는 프리미티브 값으로의 자동 변환
public class WrapperMain02 {
public static void main(String[] args) {
//기본 자료형 데이터 -> 참조 자료형 데이터 : boxing
Integer obj1 = new Integer(12);
Integer obj2 = new Integer(7);
//참조 자료형 데이터 -> 기본 자료형 데이터 : unboxing
int result = obj1.intValue()+obj2.intValue();
System.out.println("result = " + result);
System.out.println("--------------------");
//auto boxing/unboxing
Integer obj3 = 10;
Integer obj4 = 20;
Integer result2 = obj3+obj4;
System.out.println("result2 = "+result2);
}
}
Date
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateMain {
public static void main(String[] args) {
Date now = new Date();
// Mon Mar 04 14:12:09 GMT+09:00 2024
System.out.println(now);
System.out.println(now.toString());
System.out.println("-------------------");
// 2024. 3. 4. 오후 2:12:09
//deprecated 되어 사용하지 않음
System.out.println(now.toLocaleString());
System.out.println("-------------------");
DateFormat df = DateFormat.getInstance();
//24. 3. 4. 오후 2:12
String today = df.format(now);
System.out.println(today);
System.out.println("-------------------");
//2024. 3. 4. 오후 2:12:09
df = DateFormat.getDateTimeInstance();
today = df.format(now);
System.out.println(today);
System.out.println("-------------------");
// 2024. 3. 4.
df = DateFormat.getDateInstance();
today = df.format(now);
System.out.println(today);
System.out.println("-------------------");
//오후 2:14:30
df = DateFormat.getTimeInstance();
today = df.format(now);
System.out.println(today);
System.out.println("-------------------");
//2024년03월04일 (월) 오후 02:21:45
SimpleDateFormat sf = new SimpleDateFormat("yyyy년MM월dd일 (E) a hh:mm:ss");
today = sf.format(now);
System.out.println(today);
}
}
Calendar
import java.util.Calendar;
public class CalendarMain01 {
public static void main(String[] args) {
Calendar today = Calendar.getInstance();
System.out.println(today);
//2024년 3월 4일
int year = today.get(Calendar.YEAR);//연도
int month = today.get(Calendar.MONTH)+1;//월0~11
int date = today.get(Calendar.DATE); //일
System.out.printf("%d년%d월%d일%n",year,month,date);
//월요일
int day = today.get(Calendar.DAY_OF_WEEK);//요일 1~7
String[] days = {"일","월","화","수","목","금","토"};
System.out.print(days[day-1]+"요일\\n");
// 오후 3시 5분 51초
int amPm = today.get(Calendar.AM_PM); //오전 0 ,오후 1
String str = amPm==Calendar.AM ? "오전" : "오후";
int hour = today.get(Calendar.HOUR);//시 HOUR_OF_DAY(24시 표시)
int min = today.get(Calendar.MINUTE);//분
int sec = today.get(Calendar.SECOND);//초
System.out.printf("%s %d시 %d분 %d초%n", str,hour,min,sec);
}
}
StringTokenizer
import java.util.StringTokenizer;
public class StringTokenizerMain02 {
public static void main(String[] args) {
String source = "2024-03-04 16:03:20";
//구분자 여러개 지정 가능
StringTokenizer st = new StringTokenizer(source," :-");
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
'IT 국비 교육' 카테고리의 다른 글
쌍용교육센터 - 13일 (1) | 2024.03.06 |
---|---|
쌍용교육센터 - 12일 (0) | 2024.03.05 |
쌍용교육센터 - 10일 (0) | 2024.02.29 |
쌍용교육센터 - 9일 (0) | 2024.02.28 |
쌍용교육센터 - 8일 (1) | 2024.02.27 |