Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 5. 레퍼런스 데이터형.

Similar presentations


Presentation on theme: "Lesson 5. 레퍼런스 데이터형."— Presentation transcript:

1 Lesson 레퍼런스 데이터형

2 레퍼런스 데이터형이란? 기본 데이터형 레퍼런스 데이터형 int i = 5;
String s = new String("Hello"); 00 05  번지  값

3 ==과 equals() String str1 = new String("Hello");
== 연산자 변수에 저장된 값을 비교 str1 == str2  false equals() 메소드 객체에 저장된 값을 비교 str1.equals(str2)  true

4 레퍼런스 관계 1 : public class EqualRelation 2 : {
3 : public static void main(String[] args) 4 : { 5 : String str1= "Hello"; 6 : String str2= new String("Hello"); 7 : String str3= str1; 8 : String str4= str2; ...

5 String 클래스의 생성 String myString = "ultraschool"; 생성하는 방법에 따른 차이
String str1 = new String("Hello"); // 일반적인 객체 생성법  항상 객체 생성 String str2 = "Hello"; // String의 경우 =연산자로 생성이 가능  같은 문자열을 가진 객체가 있으면 레퍼런스만 함

6 String 객체의 연산 String s = "Hello"; s = s + " World";

7 String 클래스의 메소드 1 : public class StringTest 2 : {
3 : public static void main(String[] args) 4 : { 5 : String str1= "SungYong is "; 6 : String str2= "Something Else."; 7 : String str3= str1 + str2; 8 : 9 : System.out.println("str1: "+str1); 10 : System.out.println("str2: "+str2); 11 : System.out.println("str3: "+str3); 12 : System.out.println("str1의 크기는 "+ str1.length() +"입니다."); 13 : System.out.println("str2를 소문자로 바꾸면 "+ str2.toLowerCase() +"입니다."); 14 : System.out.println("str3의 g을 Q로 바꾸면 "+ str3.replace('g', 'Q') +"입니다."); 15 : System.out.println("str3의 5번째 문자는 "+ str3.charAt(4) +"입니다."); // charAt의 문자 위치는 0부터 시작 16 : } 17 : }

8 StringBuffer 클래스 StringBuffer sb = new StringBuffer();
sd.append("Hello"); String 클래스와의 차이 프로그램 실행 중에 문자열의 내용을 바꿀 수 있음 메소드 설명 append(str) 저장된 문자열 뒤에 str 추가 insert(off, str) off 위치에 str 삽입 reverse() 저장된 문자열을 반대로 변경 setCharAt(index, ch) index 위치의 문자를 ch로 변경 setLength(index) 크기를 설정 toString() String형으로 변경

9 StringTokenizer 클래스 StringTokenizer myST = new StringTokenizer("01/15/2004", "/");      클래스이름 변수 클래스이름 문자열 구분자 while(myST.hasMoreTokens()){ System.out.println(myST.nextToken()); } 아직 토큰이 남아 있는지를 조사 파싱해서 구한 다음 토큰을 반환

10 배열 배열의 선언 방법 int[ ] arr = new int[10]; 데이터형[ ] 변수; (예) int[] array;

11 배열의 초기화 int[ ] arr = { 20, 15, 3, 10, 3, 5, 4, 4, 9, 105 };

12 다차원 배열 int[ ][ ] arr = new int[3][3];

13 다차원 배열의 초기화 int[ ][ ] arr = { {1,2,3}, {4,5}, {6}};

14 Vector 클래스 배열의 단점 Vector myVector = new Vector();   
생성 후에는 크기 변경이 불가 같은 데이터형만을 저장 가능 Vector myVector = new Vector();    클래스이름 변수 클래스이름

15 Vector 클래스의 메소드 ... 7 : Vector myVector= new Vector();
10 : myVector.addElement("one"); 11 : myVector.addElement("two"); 12 : myVector.addElement(new Double(3.3)); 13 : myVector.addElement(new Boolean(true)); 18 : System.out.println("myVector의 "+ (i+1) +"번째 원소: "+ myVector.elementAt(i)); 23 : myVector.removeElementAt(1); 27 : myVector.insertElementAt("둘", 1);

16 시간과 날짜를 다루는 클래스들 Date 클래스 SimpleDateFormat 클래스 Calendar 클래스
Date today= new Date(); System.out.println(today); SimpleDateFormat 클래스 SimpleDateFormat dateForm=new SimpleDateFormat("hh시 mm분"); dateForm.format(today); Calendar 클래스 Calendar now = Calendar.getInstance(); year = now.get(Calendar.YEAR);

17 Random 클래스 Random r = new Random(); // 난수 발생 % 100  -99~99 사이 값
1 : import java.util.Random; 2 : 3 : public class MyRandom 4 : { 5 : public static void main(String[] args) 6 : { 7 : Random r= new Random(); 8 : 9 : System.out.println("1 ~ 100 범위의 난수: "+ (Math.abs(r.nextInt() % 100)+1)); 10 : } 11 : } Random r = new Random(); // 난수 발생 % 100  -99~99 사이 값 int randomNum = r.nextInt() % 100; // 음수 제거 + 1  1 ~ 100 사이 값 randomNum = Math.abs(randomNum) + 1;

18 Math 클래스 static 메소드 int y = Math.abs(-5); 메소드 설명 abs(x) x의 절대값 ceil(x)
floor(x) x보다 작거나 같은 가장 큰 정수 max(x, y) x, y 중 큰 수 min(x, y) x, y 중 작은 수 pow(x, y) x의 y승(제곱) sin(x) x 각도의 사인 값 cos(x) x 각도의 코사인 값 tan(x) x 각도의 탄젠트 값 sqrt(x) x의 평방근(루트) 8.3 참고 static 메소드 int y = Math.abs(-5); 객체 생성 없이 사용할 수 있음


Download ppt "Lesson 5. 레퍼런스 데이터형."

Similar presentations


Ads by Google