Presentation is loading. Please wait.

Presentation is loading. Please wait.

김준현.

Similar presentations


Presentation on theme: "김준현."— Presentation transcript:

1 김준현

2 java.lang 패키지 자바 프로그래밍에 가장 기본이 되는 클래스들을 포함 import 문 없이도 사용가능

3 public Boolean equals (Object obj)
객체 자신과 객체 obj가 같은 객체 인지 여부를 반환 (같으면 true) public int hashCode() 객체 자신의 해시코드를 반환

4 오버로딩(Overloading)/ 오버라이딩(Overriding)
사전적 의미 "과적하다", "과부하" "같은 클래스내에서 같은 이름의 메서드를 사용하는 것" 사용이 가능하려면 (2가지중 하나라도 만족하면 ok) (1) 매개변수 타입이 달라야 한다. (2) 매개변수 갯수가 달라야 하낟. // Instance method void func(int v) { System.out.printf("v is %d\n", v); } // overloading void func() { System.out.printf("function overloading \n");

5 오버라이딩(Overriding) "부모(Class)에서 정의한 메서드를 자식 Class에서 변경하는 것
Static 메서드는 오버라이딩을 허용하지 않는다. class Parents { // instance method void func() { System.out.println("hello world"); } class Child extends Parents { @Override System.out.println("KimJunHyeon");

6 hashCode 란? 기본구현 객체를 구별하기 위해 고유한 정수값으로 출력시켜주는 메소드
객체의 주소값을 이용해서 해시코드를 만들어 반환 따라서, 서로 다른 두 객체는 결코 같은 해시 코드를 가질 수 없음

7 toString 인스턴스에 대한 정보를 문자열(String)으로 제고알 목적으로 정의
대부분의 경우 인스턴스 변수에 저장된 값들을 문자열로 표현 원형 public String toString() { return getClass().getName() + + Integer.toHexString(hashCode()); }

8 toString class Car { // instance variable String color; int price;
Car(String c, int p) { this.color = c; this.price = p; } // 조상 클래스 (Object)에 정의된 접근성보다 같거나 넓어야 하므로, public @ Override public String toString() { return "color: " + this.color + " price: " + this.price;

9 clone() 자신을 복제하여 새로운 인스턴스를 생성 작업 실패시 원래 상태로 되돌리거나 변경되기 전의 값을 참고하는데 도움
기본 구현 단순히 인스턴스 변수의 값만 복사

10 clone()을 이용한 배열복사 배열도 객체이기 때문에 object 클래스를 상속받으며, Cloneable 인터페이스와 Serializable인터페이스가 구현 int[] TempArry = {10, 11, 12, 13, 14, 15}; int[] CloneArry = TempArry.clone();

11 class Person implements Cloneable {
// instance variable String name; int age; public Object clone() { Person a = new Person(); a.name = new String(this.name); a.age = this.age; return a; }

12 얕은 복사와 깊은 복사 얕은 복사 (shallow copy) : 단순히 객체에 저장된 값을 그대로 복제할 뿐, 객체가 참 조하고 있는 객체까지 복사하지 않는다. 깊은 복사(deep copy) : 원본이 참조하고 있는 객체까지 복사

13 얕은 복사 예) public static void main(String[] args) {
// TODO Auto-generated method stub ArrayList<String> test1 = new ArrayList<String>(); ArrayList<String> test2 = new ArrayList<String>(); test1.add("Kim"); test1.add("Jun"); test1.add("Hyeon"); test2 = test1; // 얕은 복사 - 둘 중에 한개만 수정해도 양쪽 다 적용 System.out.println(test1); test2.add("Lee"); }

14 깊은 복사 깊은 복사가 이루어져서 한쪽의 수정이 다른 한쪽에 영향을 주지 않는다.
public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> test1 = new ArrayList<String>(); ArrayList<String> test2 = new ArrayList<String>(); ArrayList<String> test3 = new ArrayList<String>(); test1.add("Kim"); test1.add("Jun"); test1.add("Hyeon"); test2 = test1; test3 = (ArrayList<String>)test1.clone(); System.out.println(test1); test2.add("Lee"); System.out.println(test3); }

15 깊은 복사의 또다른 예 // TODO Auto-generated method stub
ArrayList<String> test1 = new ArrayList<String>(); ArrayList<String> test2 = new ArrayList<String>(); ArrayList<String> test3 = new ArrayList<String>(); test1.add("Kim"); test1.add("Jun"); test1.add("Hyeon"); test2.addAll(test1); test1.add("Lee"); System.out.println(test1); System.out.println(test2);

16 Math 클래스 Math.abs(double x) Math.ceil(double x) 값 올림 Math.floor 값 내림
Math.round(double x) 반 올림 Math.max(double x, double y) 큰 쪽 반환 Math.min(double x, double y) 작은 값 반환 Math.pow(double x, double y) 제곱

17 Math.pow 반환 타입도 : double %.2f : 소수점 2번째 자리까지만 출력 public class stu02 {
public static void main(String[] args) { int x = 10; System.out.printf("10^2 => %.2f\n", Math.pow(x, 2.)); }

18 Math.ceil public class stu03 {
public static void main(String[] args) { double dData = ; System.out.printf("%f\n", Math.ceil(dData)); // }

19 래퍼 클래스 (wrapper class) 변수 기본형 (primitive) : 비객체 참조형 (reference type)
Java Data Type ㄴ Primitive Type ㄴ Boolean Type(boolean) ㄴ Numeric Type ㄴ Integral Type ㄴ Integer Type(short, int, long) ㄴ Floating Point Type(float, double) ㄴ Character Type(char) ㄴ Reference Type ㄴ Class Type ㄴ Interface Type ㄴ Array Type ㄴ Enum Type ㄴ etc.

20 래퍼 클래스 (wrapper class) 언제 사용할까??
기본형 타입을 객체로 사용해야 하는 경우, 이러한 경우에 기본형 타입 값을 객체로 포장 boxing 기본형 UnBoxing Wrapper 객체

21 래퍼 클래스 (wrapper class) Boxing UnBoxing

22 객체의 값을 double 값으로 변환하여 반환
래퍼 클래스 (wrapper class) Wrapper 클래스의 기본 메소드들 메소드 반환값 설명 booleanValue() boolean 기본형 데이터를 문자열로 바꾼 뒤에 반환 byteValue() byte 객체의 값을 byte 값으로 변환하여 반환 doubleValue() double 객체의 값을 double 값으로 변환하여 반환 floatValue() float 객체의 값을 float 값으로 변환하여 반환 intValue() int 객체의 값을 int 값으로 변환하여 반환 longValue() long 객체의 값을 long 값으로 변환하여 반환 shortValue() short 객체의 값을 short 값으로 변환하여 반환 stu05.java

23 래퍼 클래스 (wrapper class) 래퍼 클래스란
기본 자료형 (primitive data types)에 대한 클래스 표현 기본형 래퍼클래스 생성자 Boolean char Charater byte Byte short Short int Integer long Long float Float double Double void Void

24 래퍼 클래스 (wrapper class) public class stu04 {
public static void main(String[] args) { Integer iData = Integer.valueOf(10); System.out.printf("iData => %d\n", iData) }

25 자바 Scanner 클래스 Scanner 클래스 정수, 실수, 문자열을 읽어 올수 있음 nextInt() 정수 입력
nextDouble() 실수 입력 nextFloat() next() 문자열 입력 , 개행(\n)까지 읽음 stu06.java double dData = 0.; // 실수 float fData = 0.f;

26 자바 Random 선언 .nextInt() -2,147,483,648 ~2,147,483,647 .nextInt(10)
0 ~ 9 사이의 값 .nexInt(20) - 10 -10 ~ 9 .nextFloat() 0.0 (포함) ~ 1.0 (포함안함) .nextFloat()*6 0.0 (포함) ~ RN01.java

27 자바 Random import java.util.Random; public class RN02 {
public static void main(String[] args) { Random generater = new Random(); int dice; dice = generater.nextInt(6) + 1; // 1,..., 6 System.out.printf("dice => %d\n", dice); }

28 자바 정규식 regex import java.util.regex.Matcher;
import java.util.regex.Pattern; Regex Description X? X 한번 발생 혹은 전혀 발생하지 않음 X+ X 한번 혹은 그 이상 발생 X* X 제로 혹은 그 이상 발생 X{n} x 가 오직 n번 발생 X{n,} x 가 n번 혹은 그 이상 발생 X{n, m} x 가 적어도 n번 발생 하지만 m번 보다는 적음 RN04

29 자바 정규식 regex Regex Description ^ 문자열의 시작 $ 문자열의 종료 .
임의의 한 문자 (문자의 종류 가리지 않음) * 앞 문자가 없을 수도 무한정 많을 수도 있음 + 앞 문자가 하나 이상 ? 앞 문자가 없거나 하나 있음 [] 1)문자의 집합이나 범위를 나타내며 두 문자 사이는 '-' 기호로 범위를 나타낸다. 예) [A-Za-z0-9] 2)[] 내에서 ^가 선행하여 존재하면 not을 나타낸다. {} 횟수 또는 범위를 나타냄 RN05

30 자바 정규식 regex Regex Description () 소괄호 안의 문자를 하나의 문자로 인식 |
패턴 안에서 or 연산을 수행할 때 사용 \s 공백문자 \S 공백 문자가 아닌 나머지 문자 \w [A-Za-z0-9] 알파벳이나 숫자 \W ^[A-Za-z0-9] 알파벳이나 숫자를 제외한 문자 \d [0-9] 숫자 \D 숫자를 제외한 모든 문자

31 문제 번호 이메일 아이피 010-1234-1234 010-123-1234 sleep4725@naver.com

32 풀이 번호 import java.util.regex.Pattern; public class STU7 {
public static void main(String[] args) { String regExp = "(02|010)-\\d{3,4}-\\d{4}"; String data = " "; boolean result = Pattern.matches(regExp, data); if (result) { System.out.println("패턴과 일치"); } else { System.out.println("패턴과 일치 하지 않습니다."); } STU7

33 풀이 이메일 import java.util.regex.Matcher; import java.util.regex.Pattern;
public class STU8 { public static void main(String[] args) { String regExp = String target = boolean result = Pattern.matches(regExp, target ); if (result) { System.out.println("일치"); } else { System.out.println("불일치"); }

34 풀이 아이피 import java.util.regex.Pattern; public class STU9 {
public static void main(String[] args) { // String regExp = "(\\d{3})\\.(\\d{3})\\.(\\d{1,3})\\.(\\d{1,3})"; String targetIP = " "; boolean result = Pattern.matches(regExp, targetIP); if (result) { System.out.println("일치"); } else { System.out.println("불일치"); }

35 자바 정규식 regex 자주 쓰이는 패턴

36 자바 정규식 regex import java.util.regex.*; public class RN03 {
public static void main(String[] args) { //Pattern p = Pattern.compile(".S") System.out.println(Pattern.matches(".s", "as")); System.out.println(Pattern.matches(".s","mk")); }

37 자바 정규식 regex public class RN05 {
public static void main(String[] args) { Pattern p = Pattern.compile("^[A-Za-z]{2}"); System.out.println(p.matcher("AApple")); }

38 자바 정규식 regex import java.util.regex.*; public class RN06 {
public static void main(String[] args) { Pattern p = Pattern.compile("(\\d{6})-(\\d{6})"); System.out.println(p.matcher(" ")); } RN06

39 자바 정규식 regex

40 Date, Calendar Date 클래스 날짜를 표현하는 클래스 1 2 import java.util.Date;
public class STU1 { public static void main(String[] args) { System.out.println("hello world"); Date today = new Date(); // 객체 생성 String strNow = today.toString(); System.out.println(strNow); } project => STU01

41 Date, Calendar SimpleDateFormat yyyy 2018 hh 7 MM 10 mm 41 dd 28 ss 34
import java.text.SimpleDateFormat; import java.util.Date; public class STU2 { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date today = new Date(); String strData = sdf.format(today); System.out.println(strData); }

42 디렉토리 생성 (mkdir) import java.io.File; .. mkdir();
public static void main(String[] args) { File newFile = new File("C:\\Users\\sleep\\Desktop\\testStudyDir"); try { newFile.mkdir(); System.out.println("디렉토리 생성 성공"); } catch (Exception e) { System.out.println(e); }

43 디렉토리 삭제 (delete) public static void main(String[] args) {
File newFile = new File("C:\\Users\\sleep\\Desktop\\testStudyDir"); try { newFile.delete(); } catch (Exception e) { System.out.println(e); }

44 디렉토리 디렉토리 조회 (ls) import java.io.File; public class STU4 {
public static void main(String[] args) { File newFile = new File("C:\\Users\\sleep\\Desktop\\JavaScript"); File[] listOfFiles = newFile.listFiles(); for (File f : listOfFiles) { if (f.isDirectory()) { // 디렉토리 System.out.println("directory : " + f.getName()); } else if (f.isFile()) { // 일반 파일 System.out.println("file : " + f.getName());

45 문제 오늘 날짜로 디렉토리 생성

46 Calendar 클래스 Calendar 달력을 표현한 클래스
추상 (abstract) 클래스이므로 new 연산자를 사용해서 인스턴스 생성 불가 import java.util.Calendar; Calendar now = Calendar.getInstance(); now.get(Calendar.YEAR) 년도를 리턴 now.get(Calendar.MONTH)+1 월을 리턴 now.get(Calendar.DAY_OF_MONTH) 일을 리턴 now.get(Calendar.DAY_OF_WEEK) 요일을 리턴

47 Calendar 클래스 Calendar now.get(Calendar.AM_PM) 0 : 오전 / 1 : 오후
now.get(Calendar.HOUR) 현재 시간 리턴 now.get(Calendar.MINUTE) 분 리턴 now.get(Calendar.SECOND) 초 리턴 STU6.java

48 MySQL


Download ppt "김준현."

Similar presentations


Ads by Google