Data Scientist 옌

매일 발전하는 IT문제해결사

국비지원교육 (22.01-22.07)/강의노트

22-02-07(월) 008일차 [Java] char 자료형, 1차원 배열, 형변환, String클래스

옌炎 2022. 4. 10. 15:09
728x90

수업내용

수업내용


1교시 (09:30-10:20)

자바

  1. jdk
  2. 변수 데이터를 담는 상자 데이터형 변수명 = (대입연산자 : 오른쪽에 있는 값을 왼쪽에 있는 변수 상자에 담으세요)
  3. 데이터형 기초자료형 : 변수가 값을 직접 가르킨다. 숫자 byte short int long float double 문자 char 참조자료형 : 변수가 값을 직접 가르키지 않는다. 참조자료형은 객체를 가르킨다. 그 객체 안에서 데이터를 담고 있다. 문자열 string 변수 종류 Fields : 멤버 변수 상수 puvlic static final 자료형 변수명 = 값; 클래스변수 static 자료형 변수명 = 값; 전역변수 public 자료형 변수명; 멤버변수 자료형 변수명; 지역변수 자료형 변수명 = 값;
  4. 함수 데이터를 담는 상자 리턴형이 있는 함수 : 함수 블록 끝에 return 키워드를 사용해야 한다. 리턴형 함수이름(파라미터(매개변수)) {} 리턴형이 없는 함수 : 함수 블록에 return 키워드를 사용할 수 없다. void 함수이름([파라미터(매개변수)]) {} int i = add(1 + 2); => int i = 3;
  5. API 보는 방법 필드 public static int 필드명 ==> int 변수명 = 클래스이름.필드명 메소드 public static int 함수이름() ==> int 변수명 = 클래스이름.함수이름(); public int 함수이름() 클래스이름 참조변수 = new 클래스이름(); int 변수명 = 참조변수.함수이름();
  • [PPT 30p] 메모리의 주소를 문자(심볼)로 대신해서 나타내는 것을 변수(variable)라고 부르며, 프로그램에서 변수는 메모리의 주소를 의미하는 것이다.
  • [PPT 14p] 자바 가상 머신

2교시 (10:30-11:20)

package a.b.c.ch1;

// char 자료형 공부하기 
// 클래스 자원 = 변수 + 함수 
// 사용자 정의 클래스 Exam_Var_4_1 의 자원 = 변수 + 함수 (charMethod(), main())

// 자바에서 클래스에 있는 자원을 메모리에 올리는 방법
// 1. static 키워드 <-- 지양하세요. 나중에 실력이 좋아지면 쓸 것
// 2. new 키워드  연산자

public class Exam_Var_4_1 {

	void charMethod() {
		System.out.println("Character	char	2byte(양의정수 16bit, 문자를 다루기 때문에 음수를 가질 수 없다.)");

		// API 문서 확인하면서 BYTES, MAX_VALUE, MIN_VALUE, SIZE 콘솔에 출력하기
		// public static final int BYTES
		// int charBytes = java.lang.Character.BYTES;
		int charBytes = Character.BYTES;
		// public static final char MAX_VALUE
		// char charMaxValue = java.lang.Character.MAX_VALUE;
		char charMaxValue = Character.MAX_VALUE;
		// public static final char MIN_VALUE
		// char CharMinValue = java.lang.Character.MIN_VALUE;
		char charMinValue = Character.MIN_VALUE;
		// public static final char SIZE
		// int charSize = java.lang.Character.SIZE;
		int charSize = Character.SIZE;

		System.out.println("Character BYTES >>> : " + charBytes);
		System.out.println("Character MAX_VALUE >>> : " + charMaxValue);
		System.out.println("Character MIN_VALUE >>> : " + charMinValue);
		System.out.println("Character SIZE >>> : " + charSize);
		 
	}

	public static void main(String args[]) {

		// 메모리에 올리는 방법 : 클래스 전체를 올린다. : 자바에서는 클래스가 최소 단위이다. 
		// 클래스를 메모리에 올리는 방법 : 인스턴스 : instance
		// 클래스이름 참조변수 = new 클래스이름()
		Exam_Var_4_1 ev4 = new Exam_Var_4_1();
		System.out.println("ev4 참조변수 : 주소값 >>> : " + ev4);
		// System.out.println("ev4.charMethod() >>> : " + ev4.charMethod());
		ev4.charMethod();

		// new 연산자는 아래와같이 사용도 가능
		// new Exam_Var_4_1().charMethod();

	} // end of main()

} // end of Exam_Var_4_1 class

3교시 (11:30-12:20)

  • 아스키
package a.b.c.ch1;

/*
ASCII( /?æski/, 아스키)
미국정보교환표준부호(영어: American Standard Code for Information Interchange), 

자바에서는 ASCII 코드를 기초자료형 char 로 표시한다. 
문자를 char로 표시한다.
*/

// char 자료형 공부하기 
// 클래스 자원 = 변수 + 함수 
// 사용자 정의 클래스 Exam_Var_4_1 의 자원 = 변수 + 함수 (charMethod(), main())

// 자바에서 클래스에 있는 자원을 메모리에 올리는 방법
// 1. static 키워드 <-- 지양하세요. 나중에 실력이 좋아지면 쓸 것
// 2. new 키워드  연산자

public class Exam_Var_4_1 {

	void charMethod() {
		System.out.println("Character	char	2byte(양의정수 16bit, 문자를 다루기 때문에 음수를 가질 수 없다.)");

		// API 문서 확인하면서 BYTES, MAX_VALUE, MIN_VALUE, SIZE 콘솔에 출력하기
		// public static final int BYTES
		// int charBytes = java.lang.Character.BYTES;
		int charBytes = Character.BYTES;
		// public static final char MAX_VALUE
		// char charMaxValue = java.lang.Character.MAX_VALUE;
		char charMaxValue = Character.MAX_VALUE;
		// public static final char MIN_VALUE
		// char CharMinValue = java.lang.Character.MIN_VALUE;
		char charMinValue = Character.MIN_VALUE;
		// public static final char SIZE
		// int charSize = java.lang.Character.SIZE;
		int charSize = Character.SIZE;

		System.out.println("charBytes >>> : " + charBytes);
		System.out.println("charMaxValue >>> : " + charMaxValue);
		System.out.println("charMinValue >>> : " + charMinValue);
		System.out.println("charSize >>> : " + charSize);

		int charMaxValue_1 = Character.MAX_VALUE;
		int charMinValue_1 = Character.MIN_VALUE;
		System.out.println("charMaxValue_1 >>> : " + charMaxValue_1);
		System.out.println("charMinValue_1 >>> : " + charMinValue_1);


		char charMin = '\u0000'; // 유니코드
		char charMax = '\uFFFF'; // 유니코드 
		System.out.println("charMin >>> : " + charMin);
		System.out.println("charMax >>> : " + charMax);

		char charinitialization  = ' '; // char 데이터 타입은 빈 문자열, 싱글 쿼테이션으로 초기화 한다.
		System.out.println("charinitialization >>> : " + charinitialization);

	}

	public static void main(String args[]) {

		// 메모리에 올리는 방법 : 클래스 전체를 올린다. : 자바에서는 클래스가 최소 단위이다. 
		// 클래스를 메모리에 올리는 방법 : 인스턴스 : instance
		// 클래스이름 참조변수 = new 클래스이름()
		Exam_Var_4_1 ev4 = new Exam_Var_4_1();
		System.out.println("ev4 참조변수 : 주소값 >>> : " + ev4);
		// System.out.println("ev4.charMethod() >>> : " + ev4.charMethod());
		ev4.charMethod();

		// new 연산자는 아래와같이 사용도 가능
		// new Exam_Var_4_1().charMethod();

	} // end of main()

} // end of Exam_Var_4_1 class
  • 문자와 문자열

package a.b.c.ch1;

public class Exam_Var_4_2 {

	void charMethod() {
		System.out.println("Exam_Var_4_2.charMethod() 함수 시작");

		// 자료형 초기화 하기
			// char c = ''; <- 불가
			// char c = ""; <- 불가
		char c = ' ';
		System.out.println("c >>> : " + c);

		// ---------------------> 묵시적 형변환이 자동으로 발생된다.
		// byte char short int long float double
		// <-------------------- 명시적으로 형변환을 해야 한다.
		// 기초자료형에서 숫자로 사용되는 데이터타입은 서로 데이터를 주고받을 수 있다.
		// 기초자료형은 부호비트를 가지고있다.
		// 자바에서는 자료형을 변환하는 형변환을 허용한다. 연산자는 () 소괄호이다. 
		/*
		묵시적 형변환
			: 작은 자료형에서 큰 자료형으로 대입할 때 자동으로 형변환이 일어난다.
		명시적 형변환
			: 큰 자료형에서 작은 자료형으로 대입할 때는 큰 자료형을 작은 자료형으로 변환해서 대입을 해야한다.
		*/

		char ch1 = 'A';
		System.out.println("ch1 >>> : " + ch1);
		int intCh1 = ch1;
		System.out.println("intCh1 >>> : " + intCh1);
		char ch1_1 = (char)intCh1;
		System.out.println("ch1_1 >>> : " + ch1_1);

	}

	public static void main(String args[]) {
		
		new Exam_Var_4_2().charMethod();

	} // end of main()

} // end of Exam_Var_4_2 class

4교시 (12:30-13:20)

  • [교재 52p] 문자 자료형

5교시 (14:30-15:20)

  • 인덱스, 첨자
    • 단 인덱스는 0부터 시작을 한다. (예외: 오라클 데이터베이스)
    package a.b.c.ch1;
    
    /*
    1. 문자열		문자의 배열
    	자바에서는 String 클래스로 문자열을 다룬다.
    	String 클래스는 char의 배열이다. 
    2. 배열 
    	데이터를 순차적(시퀀스 sequence, index, 첨자)으로 나열해 놓은 것 
    */
    
    public class Exam_Var_5 {
    
    	public void stringTest(){
    		
    		/*
    		1. 문자열을 다루는 클래스 String 선언하고
    		2. String 클래스의 변수명을 str 선언하고
    		3. = : 대입연산자 선언하고
    		4. "abc" 문자열을 str 변수에 대입을 한다. 
    		*/
    		String str = "abc";
    		System.out.println("str >>> : " + str);
    
    		// public char charAt(int index)
    		char ch0 = str.charAt(0);
    		char ch1 = str.charAt(1);
    		char ch2 = str.charAt(2);
    		
    		// abc 에 없는 데이터인데 컴파일 할 때는 에러가 나지 않는다.
    		//char ch3 = str.charAt(3);
    
    		System.out.println("ch0 >>> : " + ch0);
    		System.out.println("ch1 >>> : " + ch1);
    		System.out.println("ch2 >>> : " + ch2);
    
    		// abc 에 없는 데이터인데 컴파일 할 때는 에러가 나지 않고
    		// 실행할 때 에러가 발생된다.
    		// System.out.println("ch3 >>> : " + ch3);
    
    		String str2 = "강소영";
    		System.out.println("str2 >>> : " + str2);
    		char ch20 = str2.charAt(0);
    		char ch21 = str2.charAt(1);
    		char ch22 = str2.charAt(2);
    		System.out.println("ch20 >>> : " + ch20);
    		System.out.println("ch21 >>> : " + ch21);
    		System.out.println("ch22 >>> : " + ch22);
    
    	}		
    
    	public static void main(String args[]) {
    
    		new Exam_Var_5().stringTest();
    	} // end of mai()
    } // end of Exam_Var_5
    

6교시 (15:30-16:20)

  • 1차원 배열
package a.b.c.ch1;

// String 클래스 : 문자열을 다루는 클래스 : char 배열이다. 문자의 배열이다.

// 배열 : 데이터를 순차적으로 관리하는 객체
// 배열 연산자 [] 대괄호 braket 브라켓
// 배열 : 1차원 배열, 2차원 배열.... 가변 배열
// 1차원 배열
// 데이터형 참조변수[] 배열연산자 = new 데이터형[배열이 들어갈 공간] 배열연산자;
// 데이터형 참조변수[] = new 데이터형[]{};
// 데이터형 참조변수[] = {};

public class Exam_String {

	public void arrayTest() {
		System.out.println("Exam_String.array.Test() 함수 시작>>> : ");

		// char(차) 데이터타입의 배열이다.
		char cArray[] = {'a', 'b', 'c'};
		System.out.println("cArray >>> : " + cArray);
		System.out.println("cArray[0] >>> : " + cArray[0]);
		System.out.println("cArray[1] >>> : " + cArray[1]);
		System.out.println("cArray[2] >>> : " + cArray[2]);

		// String 클래스(문자열 클래스)를 인스턴스 했다.
		String str = new String(cArray);
		System.out.println("str >>> : " + str);
		System.out.println("str.charAt(0) >>> : " + str.charAt(0));
		System.out.println("str.charAt(1) >>> : " + str.charAt(1));
		System.out.println("str.charAt(2) >>> : " + str.charAt(2));

		// String 클래스(문자열 클래스)를 인스턴스 했다.
		String str1 = new String("abc");
		System.out.println("str1 >>> : " + str1);
		System.out.println("str1.charAt(0) >>> : " + str1.charAt(0));
		System.out.println("str1.charAt(1) >>> : " + str1.charAt(1));
		System.out.println("str1.charAt(2) >>> : " + str1.charAt(2));

		//===================================================================
		// 이것만 사용하면 된다.
		String str2 = "abc";
		System.out.println("str2 >>> : " + str2);
		System.out.println("str2.charAt(0) >>> : " + str2.charAt(0));
		System.out.println("str2.charAt(1) >>> : " + str2.charAt(1));
		System.out.println("str2.charAt(2) >>> : " + str2.charAt(2));
		//===================================================================

	} // end of arrayTest()
	
	public static void main(String args[]) {

		new Exam_String().arrayTest();

	} // end of main()

} // end of Exam_String class

7교시 (16:30-17:20)

  • [교재 369p] String 클래스
    package a.b.c.ch1;
    
    
    public class Exam_String_1 {
    
    	public void arrayTest() {
    		System.out.println("Exam_String.arrayTest() 함수 시작 >>> : ");
    
    		// java.lang.System
    		// public static int identityHashCode(Object x)
    
    		// char(차) 데이터타입의 배열이다.
    		char cArray[] = {'a', 'b', 'c'};
    		System.out.println("cArray >>> : " + cArray);
    		System.out.println("System.identityHashCode(cArray) >>> : " 
    						+ System.identityHashCode(cArray));
    
    		// String 클래스(문자열 클래스)를 인스턴스 했다.
    		String str = new String(cArray);
    		System.out.println("str >>> : " + str);
    		System.out.println("System.identityHashCode(str) >>> : " 
    						+ System.identityHashCode(str));
    
    		// String 클래스(문자열 클래스)를 인스턴스 했다.
    		String str1 = new String("abc");
    		System.out.println("str1 >>> : " + str1);
    		System.out.println("System.identityHashCode(str1) >>> : " 
    						+ System.identityHashCode(str1));
    
    		//===========================================================
    		// 이것만 사용하면 된다.
    		String str2 = "abc";
    		System.out.println("str2 >>> : " + str2);
    		System.out.println("System.identityHashCode(str2) >>> : " 
    						+ System.identityHashCode(str2));
    		//===========================================================
    	}
    
    	public static void main(String args[]) {
    		
    		new Exam_String_1().arrayTest();
    	}
    }
  • package a.b.c.ch1; public class Exam_String_1 { public void arrayTest() { System.out.println("Exam_String.arrayTest() 함수 시작 >>> : "); // java.lang.System // public static int identityHashCode(Object x) // char(차) 데이터타입의 배열이다. char cArray[] = {'a', 'b', 'c'}; System.out.println("cArray >>> : " + cArray); System.out.println("System.identityHashCode(cArray) >>> : " + System.identityHashCode(cArray)); // String 클래스(문자열 클래스)를 인스턴스 했다. String str = new String(cArray); System.out.println("str >>> : " + str); System.out.println("System.identityHashCode(str) >>> : " + System.identityHashCode(str)); // String 클래스(문자열 클래스)를 인스턴스 했다. String str1 = new String("abc"); System.out.println("str1 >>> : " + str1); System.out.println("System.identityHashCode(str1) >>> : " + System.identityHashCode(str1)); //=========================================================== // 이것만 사용하면 된다. String str2 = "abc"; System.out.println("str2 >>> : " + str2); System.out.println("System.identityHashCode(str2) >>> : " + System.identityHashCode(str2)); //=========================================================== } public static void main(String args[]) { new Exam_String_1().arrayTest(); } }

8교시 (17:30-18:30)

package a.b.c.ch1;

public class Exam_String_3 {

	public void stringTest() {
		System.out.println("Exam_String_2.stringTest() 함수 시작 >>> : ");

		/*
		1. String 클래스 선언
		2. String 클래스 사용하려고 str0 변수 선언
		3. = : 대입 연산자 선언 : 오른쪽에 있는 값을 왼쪽에 있는 변수명에 대입한다.
		4. "" : 리터럴 (문자) : 빈문자열
		5. ; 문자 종결 연산자 
		6. str0  변수에 빈문자열을 대입한다. 
		*/
		String str0 = ""; // 빈문자열로 초기화 하는 방법
		//  str0 참조변수를 선언해서 도트연산자로 String 클래스에 있는 length() 함수를 호출했다.
		//  결과 값 str0 의 대입한  "" 빈 문자열의 값을 가지고 있다. 
		str0.length();

		// System.out.println() 문장으로 콘솔에 출력을 한다.
		System.out.println("" + str0.length());

		// str0.length() 호출한 결과 값을 str0Length 변수에 대입한다. 활당한다. 치환한다.
		int str0Length = str0.length();
		// 위 표현식이 수행되고 난 후에는 str0Length 변수 0 값이 대입되어 있다. 

		// System.out.println() 문장으로 str0Length 변수에 대입된 값을 콘솔에 출력한다. 
		System.out.println("" + str0Length);
	}

	public static void main(String args[]) {
		
		new Exam_String_3().stringTest();
	}
}

Notes

  • 다른 사람과 비교하지 말고 내 속도와 체력에 맞춰서 하기
    • 나의 상태를 아는 것이 중요
  • 먼저 사례를 보면서 비슷한 구조를 찾은 후에 나중에 논리를 확인하면 더 쉬움


1교시 (09:30-10:20)

자바

  1. jdk
  2. 변수 데이터를 담는 상자 데이터형 변수명 = (대입연산자 : 오른쪽에 있는 값을 왼쪽에 있는 변수 상자에 담으세요)
  3. 데이터형 기초자료형 : 변수가 값을 직접 가르킨다. 숫자 byte short int long float double 문자 char 참조자료형 : 변수가 값을 직접 가르키지 않는다. 참조자료형은 객체를 가르킨다. 그 객체 안에서 데이터를 담고 있다. 문자열 string 변수 종류 Fields : 멤버 변수 상수 puvlic static final 자료형 변수명 = 값; 클래스변수 static 자료형 변수명 = 값; 전역변수 public 자료형 변수명; 멤버변수 자료형 변수명; 지역변수 자료형 변수명 = 값;
  4. 함수 데이터를 담는 상자 리턴형이 있는 함수 : 함수 블록 끝에 return 키워드를 사용해야 한다. 리턴형 함수이름(파라미터(매개변수)) {} 리턴형이 없는 함수 : 함수 블록에 return 키워드를 사용할 수 없다. void 함수이름([파라미터(매개변수)]) {} int i = add(1 + 2); => int i = 3;
  5. API 보는 방법 필드 public static int 필드명 ==> int 변수명 = 클래스이름.필드명 메소드 public static int 함수이름() ==> int 변수명 = 클래스이름.함수이름(); public int 함수이름() 클래스이름 참조변수 = new 클래스이름(); int 변수명 = 참조변수.함수이름();
  • [PPT 30p] 메모리의 주소를 문자(심볼)로 대신해서 나타내는 것을 변수(variable)라고 부르며, 프로그램에서 변수는 메모리의 주소를 의미하는 것이다.
  • [PPT 14p] 자바 가상 머신

2교시 (10:30-11:20)

package a.b.c.ch1;

// char 자료형 공부하기 
// 클래스 자원 = 변수 + 함수 
// 사용자 정의 클래스 Exam_Var_4_1 의 자원 = 변수 + 함수 (charMethod(), main())

// 자바에서 클래스에 있는 자원을 메모리에 올리는 방법
// 1. static 키워드 <-- 지양하세요. 나중에 실력이 좋아지면 쓸 것
// 2. new 키워드  연산자

public class Exam_Var_4_1 {

	void charMethod() {
		System.out.println("Character	char	2byte(양의정수 16bit, 문자를 다루기 때문에 음수를 가질 수 없다.)");

		// API 문서 확인하면서 BYTES, MAX_VALUE, MIN_VALUE, SIZE 콘솔에 출력하기
		// public static final int BYTES
		// int charBytes = java.lang.Character.BYTES;
		int charBytes = Character.BYTES;
		// public static final char MAX_VALUE
		// char charMaxValue = java.lang.Character.MAX_VALUE;
		char charMaxValue = Character.MAX_VALUE;
		// public static final char MIN_VALUE
		// char CharMinValue = java.lang.Character.MIN_VALUE;
		char charMinValue = Character.MIN_VALUE;
		// public static final char SIZE
		// int charSize = java.lang.Character.SIZE;
		int charSize = Character.SIZE;

		System.out.println("Character BYTES >>> : " + charBytes);
		System.out.println("Character MAX_VALUE >>> : " + charMaxValue);
		System.out.println("Character MIN_VALUE >>> : " + charMinValue);
		System.out.println("Character SIZE >>> : " + charSize);
		 
	}

	public static void main(String args[]) {

		// 메모리에 올리는 방법 : 클래스 전체를 올린다. : 자바에서는 클래스가 최소 단위이다. 
		// 클래스를 메모리에 올리는 방법 : 인스턴스 : instance
		// 클래스이름 참조변수 = new 클래스이름()
		Exam_Var_4_1 ev4 = new Exam_Var_4_1();
		System.out.println("ev4 참조변수 : 주소값 >>> : " + ev4);
		// System.out.println("ev4.charMethod() >>> : " + ev4.charMethod());
		ev4.charMethod();

		// new 연산자는 아래와같이 사용도 가능
		// new Exam_Var_4_1().charMethod();

	} // end of main()

} // end of Exam_Var_4_1 class

3교시 (11:30-12:20)

  • 아스키
  • package a.b.c.ch1; /* ASCII( /?æski/, 아스키) 미국정보교환표준부호(영어: American Standard Code for Information Interchange), 자바에서는 ASCII 코드를 기초자료형 char 로 표시한다. 문자를 char로 표시한다. */ // char 자료형 공부하기 // 클래스 자원 = 변수 + 함수 // 사용자 정의 클래스 Exam_Var_4_1 의 자원 = 변수 + 함수 (charMethod(), main()) // 자바에서 클래스에 있는 자원을 메모리에 올리는 방법 // 1. static 키워드 <-- 지양하세요. 나중에 실력이 좋아지면 쓸 것 // 2. new 키워드 연산자 public class Exam_Var_4_1 { void charMethod() { System.out.println("Character char 2byte(양의정수 16bit, 문자를 다루기 때문에 음수를 가질 수 없다.)"); // API 문서 확인하면서 BYTES, MAX_VALUE, MIN_VALUE, SIZE 콘솔에 출력하기 // public static final int BYTES // int charBytes = java.lang.Character.BYTES; int charBytes = Character.BYTES; // public static final char MAX_VALUE // char charMaxValue = java.lang.Character.MAX_VALUE; char charMaxValue = Character.MAX_VALUE; // public static final char MIN_VALUE // char CharMinValue = java.lang.Character.MIN_VALUE; char charMinValue = Character.MIN_VALUE; // public static final char SIZE // int charSize = java.lang.Character.SIZE; int charSize = Character.SIZE; System.out.println("charBytes >>> : " + charBytes); System.out.println("charMaxValue >>> : " + charMaxValue); System.out.println("charMinValue >>> : " + charMinValue); System.out.println("charSize >>> : " + charSize); int charMaxValue_1 = Character.MAX_VALUE; int charMinValue_1 = Character.MIN_VALUE; System.out.println("charMaxValue_1 >>> : " + charMaxValue_1); System.out.println("charMinValue_1 >>> : " + charMinValue_1); char charMin = '\\u0000'; // 유니코드 char charMax = '\\uFFFF'; // 유니코드 System.out.println("charMin >>> : " + charMin); System.out.println("charMax >>> : " + charMax); char charinitialization = ' '; // char 데이터 타입은 빈 문자열, 싱글 쿼테이션으로 초기화 한다. System.out.println("charinitialization >>> : " + charinitialization); } public static void main(String args[]) { // 메모리에 올리는 방법 : 클래스 전체를 올린다. : 자바에서는 클래스가 최소 단위이다. // 클래스를 메모리에 올리는 방법 : 인스턴스 : instance // 클래스이름 참조변수 = new 클래스이름() Exam_Var_4_1 ev4 = new Exam_Var_4_1(); System.out.println("ev4 참조변수 : 주소값 >>> : " + ev4); // System.out.println("ev4.charMethod() >>> : " + ev4.charMethod()); ev4.charMethod(); // new 연산자는 아래와같이 사용도 가능 // new Exam_Var_4_1().charMethod(); } // end of main() } // end of Exam_Var_4_1 class
  • 문자와 문자열
    package a.b.c.ch1;
    
    public class Exam_Var_4_2 {
    
    	void charMethod() {
    		System.out.println("Exam_Var_4_2.charMethod() 함수 시작");
    
    		// 자료형 초기화 하기
    			// char c = ''; <- 불가
    			// char c = ""; <- 불가
    		char c = ' ';
    		System.out.println("c >>> : " + c);
    
    		// ---------------------> 묵시적 형변환이 자동으로 발생된다.
    		// byte char short int long float double
    		// <-------------------- 명시적으로 형변환을 해야 한다.
    		// 기초자료형에서 숫자로 사용되는 데이터타입은 서로 데이터를 주고받을 수 있다.
    		// 기초자료형은 부호비트를 가지고있다.
    		// 자바에서는 자료형을 변환하는 형변환을 허용한다. 연산자는 () 소괄호이다. 
    		/*
    		묵시적 형변환
    			: 작은 자료형에서 큰 자료형으로 대입할 때 자동으로 형변환이 일어난다.
    		명시적 형변환
    			: 큰 자료형에서 작은 자료형으로 대입할 때는 큰 자료형을 작은 자료형으로 변환해서 대입을 해야한다.
    		*/
    
    		char ch1 = 'A';
    		System.out.println("ch1 >>> : " + ch1);
    		int intCh1 = ch1;
    		System.out.println("intCh1 >>> : " + intCh1);
    		char ch1_1 = (char)intCh1;
    		System.out.println("ch1_1 >>> : " + ch1_1);
    
    	}
    
    	public static void main(String args[]) {
    		
    		new Exam_Var_4_2().charMethod();
    
    	} // end of main()
    
    } // end of Exam_Var_4_2 class
    

4교시 (12:30-13:20)

  • [교재 52p] 문자 자료형

5교시 (14:30-15:20)

  • 인덱스, 첨자
    • 단 인덱스는 0부터 시작을 한다. (예외: 오라클 데이터베이스)
    package a.b.c.ch1;
    
    /*
    1. 문자열		문자의 배열
    	자바에서는 String 클래스로 문자열을 다룬다.
    	String 클래스는 char의 배열이다. 
    2. 배열 
    	데이터를 순차적(시퀀스 sequence, index, 첨자)으로 나열해 놓은 것 
    */
    
    public class Exam_Var_5 {
    
    	public void stringTest(){
    		
    		/*
    		1. 문자열을 다루는 클래스 String 선언하고
    		2. String 클래스의 변수명을 str 선언하고
    		3. = : 대입연산자 선언하고
    		4. "abc" 문자열을 str 변수에 대입을 한다. 
    		*/
    		String str = "abc";
    		System.out.println("str >>> : " + str);
    
    		// public char charAt(int index)
    		char ch0 = str.charAt(0);
    		char ch1 = str.charAt(1);
    		char ch2 = str.charAt(2);
    		
    		// abc 에 없는 데이터인데 컴파일 할 때는 에러가 나지 않는다.
    		//char ch3 = str.charAt(3);
    
    		System.out.println("ch0 >>> : " + ch0);
    		System.out.println("ch1 >>> : " + ch1);
    		System.out.println("ch2 >>> : " + ch2);
    
    		// abc 에 없는 데이터인데 컴파일 할 때는 에러가 나지 않고
    		// 실행할 때 에러가 발생된다.
    		// System.out.println("ch3 >>> : " + ch3);
    
    		String str2 = "강소영";
    		System.out.println("str2 >>> : " + str2);
    		char ch20 = str2.charAt(0);
    		char ch21 = str2.charAt(1);
    		char ch22 = str2.charAt(2);
    		System.out.println("ch20 >>> : " + ch20);
    		System.out.println("ch21 >>> : " + ch21);
    		System.out.println("ch22 >>> : " + ch22);
    
    	}		
    
    	public static void main(String args[]) {
    
    		new Exam_Var_5().stringTest();
    	} // end of mai()
    } // end of Exam_Var_5
    

6교시 (15:30-16:20)

  • 1차원 배열
package a.b.c.ch1;

// String 클래스 : 문자열을 다루는 클래스 : char 배열이다. 문자의 배열이다.

// 배열 : 데이터를 순차적으로 관리하는 객체
// 배열 연산자 [] 대괄호 braket 브라켓
// 배열 : 1차원 배열, 2차원 배열.... 가변 배열
// 1차원 배열
// 데이터형 참조변수[] 배열연산자 = new 데이터형[배열이 들어갈 공간] 배열연산자;
// 데이터형 참조변수[] = new 데이터형[]{};
// 데이터형 참조변수[] = {};

public class Exam_String {

	public void arrayTest() {
		System.out.println("Exam_String.array.Test() 함수 시작>>> : ");

		// char(차) 데이터타입의 배열이다.
		char cArray[] = {'a', 'b', 'c'};
		System.out.println("cArray >>> : " + cArray);
		System.out.println("cArray[0] >>> : " + cArray[0]);
		System.out.println("cArray[1] >>> : " + cArray[1]);
		System.out.println("cArray[2] >>> : " + cArray[2]);

		// String 클래스(문자열 클래스)를 인스턴스 했다.
		String str = new String(cArray);
		System.out.println("str >>> : " + str);
		System.out.println("str.charAt(0) >>> : " + str.charAt(0));
		System.out.println("str.charAt(1) >>> : " + str.charAt(1));
		System.out.println("str.charAt(2) >>> : " + str.charAt(2));

		// String 클래스(문자열 클래스)를 인스턴스 했다.
		String str1 = new String("abc");
		System.out.println("str1 >>> : " + str1);
		System.out.println("str1.charAt(0) >>> : " + str1.charAt(0));
		System.out.println("str1.charAt(1) >>> : " + str1.charAt(1));
		System.out.println("str1.charAt(2) >>> : " + str1.charAt(2));

		//===================================================================
		// 이것만 사용하면 된다.
		String str2 = "abc";
		System.out.println("str2 >>> : " + str2);
		System.out.println("str2.charAt(0) >>> : " + str2.charAt(0));
		System.out.println("str2.charAt(1) >>> : " + str2.charAt(1));
		System.out.println("str2.charAt(2) >>> : " + str2.charAt(2));
		//===================================================================

	} // end of arrayTest()
	
	public static void main(String args[]) {

		new Exam_String().arrayTest();

	} // end of main()

} // end of Exam_String class

7교시 (16:30-17:20)

  • [교재 369p] String 클래스
package a.b.c.ch1;


public class Exam_String_1 {

	public void arrayTest() {
		System.out.println("Exam_String.arrayTest() 함수 시작 >>> : ");

		// java.lang.System
		// public static int identityHashCode(Object x)

		// char(차) 데이터타입의 배열이다.
		char cArray[] = {'a', 'b', 'c'};
		System.out.println("cArray >>> : " + cArray);
		System.out.println("System.identityHashCode(cArray) >>> : " 
						+ System.identityHashCode(cArray));

		// String 클래스(문자열 클래스)를 인스턴스 했다.
		String str = new String(cArray);
		System.out.println("str >>> : " + str);
		System.out.println("System.identityHashCode(str) >>> : " 
						+ System.identityHashCode(str));

		// String 클래스(문자열 클래스)를 인스턴스 했다.
		String str1 = new String("abc");
		System.out.println("str1 >>> : " + str1);
		System.out.println("System.identityHashCode(str1) >>> : " 
						+ System.identityHashCode(str1));

		//===========================================================
		// 이것만 사용하면 된다.
		String str2 = "abc";
		System.out.println("str2 >>> : " + str2);
		System.out.println("System.identityHashCode(str2) >>> : " 
						+ System.identityHashCode(str2));
		//===========================================================
	}

	public static void main(String args[]) {
		
		new Exam_String_1().arrayTest();
	}
}
package a.b.c.ch1;

/*
1. ASCII(American Standard Code for Information Interchange)
	ASCII는 최초의 문자열 인코딩이다.
	7 bit로 구성되어 있으며, 영어를 위한 문자, 숫자, 특수문자, 기호 등 128개 문자를 표현할 수 있다.

2. ANSI(American National Standard Institute)
	ANSI는 8bit로 구성되어 있으며 256개의 문자를 표현할 수 있다.
	ANSI의 앞 7bit는 ASCII와 동일하고, 뒤에 1bit를 이용하여 다른 언어의 문자를 표현한다.
*/

public class Exam_String_2 {
	
	public void stringTest() {
		
		System.out.println("Exam_String_2.stringTest() 함수 시작 >>> : ");
		System.out.println("String 클래스에서 문자열 더블 쿼테이션을 사용한다.");

		// 스트링 클래스를 초기화하는 방법
		String str0 = ""; // 빈 문자열로 초기화하는 방법
		String str1 = null; // null로 초기화하는 방법 : default 값
							// 데이터가 없다는 의미이므로 데이터를 초기화(데이터 값) 해서 사용해야 한다.
		String str2 = " ab c  ";

		// public int length()
		System.out.println("str0.length() 문자열 길이를 구하는 함수 >>> : " + str0.length());
		int str0Length = str0.length();
		System.out.println("str0Length 문자열 길이를 구하는 함수 >>> : " + str0Length);
		System.out.println("str2.length() 문자열 길이를 구하는 함수 >>> : " + str2.length());
		int str2Length = str2.length();
		System.out.println("str2Length 문자열 길이를 구하는 함수 >>> : " + str2Length);

		// System.out.println("str1.length() 문자열 길이를 구하는 함수 >>> : " + str1.length());
		/*
			Exception in thread "main" java.lang.NullPointerException
	        at a.b.c.ch1.Exam_String_2.stringTest(Exam_String_2.java:22)
	        at a.b.c.ch1.Exam_String_2.main(Exam_String_2.java:29
		*/
		System.out.println("str0 >>> : " + str0);
		System.out.println("str1 >>> : " + str1);
		System.out.println("str2 >>> : " + str2);
		
		// System.out.println("str0.charAt(0) >>> : " + str0.charAt(0));
		/*
			Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
			at java.lang.String.charAt(String.java:658)
			at a.b.c.ch1.Exam_String_2.stringTest(Exam_String_2.java:34)
			at a.b.c.ch1.Exam_String_2.main(Exam_String_2.java:41)
		*/

		// System.out.println("str1.charAt(0) >>> : " + str1.charAt(0));
		/*
			Exception in thread "main" java.lang.NullPointerException
	        at a.b.c.ch1.Exam_String_2.stringTest(Exam_String_2.java:41)
	        at a.b.c.ch1.Exam_String_2.main(Exam_String_2.java:47)		
		*/

		System.out.println("str2.charAt(0) >>> : " + str2.charAt(0));
		System.out.println("(int)str2.charAt(0) >>> : " + (int)str2.charAt(0));
		// pulbic static String toHexString(int i)
		System.out.println("Integer.toHexString(str2.charAt(0)) >>> : "
							+ Integer.toHexString(str2.charAt(0)));
		System.out.println("Integer.toHexString(str2.charAt(1)) >>> : "
							+ Integer.toHexString(str2.charAt(1)));
		System.out.println("Integer.toHexString(str2.charAt(2)) >>> : "
							+ Integer.toHexString(str2.charAt(2)));
		System.out.println("Integer.toHexString(str2.charAt(3)) >>> : "
							+ Integer.toHexString(str2.charAt(3)));
		System.out.println("Integer.toHexString(str2.charAt(4)) >>> : "
							+ Integer.toHexString(str2.charAt(4)));
		System.out.println("Integer.toHexString(str2.charAt(5)) >>> : "
							+ Integer.toHexString(str2.charAt(5)));
		System.out.println("Integer.toHexString(str2.charAt(6)) >>> : "
							+ Integer.toHexString(str2.charAt(6)));
	
	} // end of stringTest()
	
	public static void main(String args[]) {

		new Exam_String_2().stringTest();

	} // end of main()

} // end of Exam_String_2 class

8교시 (17:30-18:30)

package a.b.c.ch1;

public class Exam_String_3 {

	public void stringTest() {
		System.out.println("Exam_String_2.stringTest() 함수 시작 >>> : ");

		/*
		1. String 클래스 선언
		2. String 클래스 사용하려고 str0 변수 선언
		3. = : 대입 연산자 선언 : 오른쪽에 있는 값을 왼쪽에 있는 변수명에 대입한다.
		4. "" : 리터럴 (문자) : 빈문자열
		5. ; 문자 종결 연산자 
		6. str0  변수에 빈문자열을 대입한다. 
		*/
		String str0 = ""; // 빈문자열로 초기화 하는 방법
		//  str0 참조변수를 선언해서 도트연산자로 String 클래스에 있는 length() 함수를 호출했다.
		//  결과 값 str0 의 대입한  "" 빈 문자열의 값을 가지고 있다. 
		str0.length();

		// System.out.println() 문장으로 콘솔에 출력을 한다.
		System.out.println("" + str0.length());

		// str0.length() 호출한 결과 값을 str0Length 변수에 대입한다. 활당한다. 치환한다.
		int str0Length = str0.length();
		// 위 표현식이 수행되고 난 후에는 str0Length 변수 0 값이 대입되어 있다. 

		// System.out.println() 문장으로 str0Length 변수에 대입된 값을 콘솔에 출력한다. 
		System.out.println("" + str0Length);
	}

	public static void main(String args[]) {
		
		new Exam_String_3().stringTest();
	}
}

Notes

  • 다른 사람과 비교하지 말고 내 속도와 체력에 맞춰서 하기
    • 나의 상태를 아는 것이 중요
  • 먼저 사례를 보면서 비슷한 구조를 찾은 후에 나중에 논리를 확인하면 더 쉬움

728x90