Ex01_Object
package com.mywork.ex;
class Sample {
@Override
public String toString() {
return "Sample";
}
}
public class Ex01_Object {
public static void main(String[] args) {
Object object = new Object();
object = 10;
System.out.println(object);
object = "HELLO";
System.out.println(object);
object = new Sample();
System.out.println(object.toString());
System.out.println(object);
}
}
Ex02_Object
package com.mywork.ex;
class Computer{
private String model;
private int price;
public Computer(String model, int price) {
this.model = model;
this.price = price;
}
@Override
public String toString() {
String info = "모델 : " + model + ", 가격 : " + price + "만원";
return info;
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Computer) {
return model.equals(((Computer)obj).model) && price == ((Computer)obj).price;
}else{
return false;
}
}
}
public class Ex02_Object {
public static void main(String[] args) {
Computer myComputer1 = new Computer("삼성센스", 100);
Computer myComputer2 = new Computer("삼성센스", 100);
System.out.println(myComputer1);
System.out.println(myComputer2);
if(myComputer1 == myComputer2) {
System.out.println("같은 종류의 컴퓨터이다.");
}else {
System.out.println("다른 종류의 컴퓨터이다.");
}
if(myComputer1.equals(myComputer2)) {
System.out.println("같은 종류의 컴퓨터이다.");
}else {
System.out.println("다른 종류의 컴퓨터이다.");
}
}
}
Ex03_Object
package com.mywork.ex;
class Person implements Cloneable{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "이름 : " + name + ", 나이 : " + age + "살";
}
@Override
public Object clone() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
public class Ex04_Object {
public static void main(String[] args) {
Person woman = new Person("alice", 20);
System.out.println(woman);
Person cloneWoman = (Person)woman.clone();
System.out.println(cloneWoman);
}
}
Ex04_String
package com.mywork.ex;
public class Ex05_String {
public static void main(String[] args) {
String a = "apple";
String b = "apple";
System.out.println(a == b ? "apple 1개" : "apple 2개");
String c = new String("banana");
String d = new String("banana");
System.out.println(c == d ? "banana 1개" : "banana 2개");
String sn = "951212-1234567";
String[] snArr = sn.split("-");
for(String s : snArr) {
System.out.println(s);
}
String today = "2019.10.27";
String[] todayArr = today.split("\\.");
for(String t : todayArr) {
System.out.println(t);
}
String today2 = String.join("-", todayArr);
System.out.println(today2);
String phone = "010-1234-5678";
String phone1 = phone.substring(0, 3);
String phone2 = phone.substring(4, 8);
String phone3 = phone.substring(9);
System.out.println(phone1);
System.out.println(phone2);
System.out.println(phone3);
}
}
Ex05_StringBuffer
package com.mywork.ex;
public class Ex06_StringBuffer {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("apple");
StringBuffer sb2 = new StringBuffer("apple");
System.out.println("sb1 해시코드 값(주소값)=" + sb1.hashCode());
System.out.println("sb2 해시코드 값(주소값)=" + sb2.hashCode());
StringBuffer sb = new StringBuffer();
System.out.println("기본 버퍼크기 : " + sb.capacity());
sb.append("hello");
sb.append(" java").append(" world");
System.out.println(sb);
StringBuffer phone = new StringBuffer();
phone.append("010-1234-5678");
phone.deleteCharAt(3);
phone.deleteCharAt(7);
System.out.println(phone);
}
}
Ex07_Wrapper
package com.mywork.ex;
public class Ex07_Wrapper {
public static void main(String[] args) {
Integer a = new Integer(10);
Integer b = new Integer(1);
System.out.println("저장된 위치 비교 : " + (a == b));
System.out.println("저장된 내용 비교 : " + a.equals(b));
System.out.println("두 객체에 저장된 값의 크기 비교 : " + a.compareTo(b));
Integer c = 100;
Integer d = 200;
int result = c + d;
System.out.println(result);
}
}
Ex08_Big_number
package com.mywork.ex;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Ex08_Big_number {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MAX_VALUE);
BigInteger a = new BigInteger("12345678901234567890");
BigInteger b = new BigInteger("123456789012345678901234567890");
System.out.println(BigInteger.ZERO);
System.out.println(BigInteger.ONE);
System.out.println(BigInteger.TEN);
System.out.println(a.add(b));
System.out.println(a.subtract(b));
System.out.println(a.multiply(b));
System.out.println(a.divide(b));
double d = 1.123456789123456789;
System.out.println(d);
BigDecimal e = new BigDecimal("1.123456789123456789");
System.out.println(e);
}
}
Ex09_Calendar
package com.mywork.ex;
import java.util.Calendar;
public class Ex09_Calendar {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(2019, 9, 21, 11, 34, 10);
System.out.println("년 : " + cal.get(Calendar.YEAR)) ;
System.out.println("월 : " + (cal.get(Calendar.MONTH) + 1)) ;
System.out.println("일 : " + cal.get(Calendar.DAY_OF_MONTH)) ;
System.out.println("요일번호 : " + cal.get(Calendar.DAY_OF_WEEK));
System.out.println("오전/오후 : " + cal.get(Calendar.AM_PM));
System.out.println("시 : " + cal.get(Calendar.HOUR));
System.out.println("시 : " + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("분 : " + cal.get(Calendar.MINUTE));
System.out.println("초 : " + cal.get(Calendar.SECOND));
System.out.println("밀리 초 : " + cal.get(Calendar.MILLISECOND));
System.out.println("밀리 초->초 : " + (cal.get(Calendar.MILLISECOND) / 1000.0) + "초");
System.out.println(cal.getTimeInMillis());
}
}
package com.mywork.ex;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Ex10_Date_SimpleDateFormat {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 E요일");
String today = sdf.format(now);
System.out.println(today);
}
}
package com.mywork.ex;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Ex11_Calendar_Date_SimpleDataFormat {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("a hh시 mm분 ss초");
String today = sdf.format(now);
System.out.println(today);
}
}