2016. 8. 4. 09:31ㆍProgramming/Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class L03Number { public static void main(String[] args) { byte a = 127; //1byte = 8bit, 2의 8승이므로 256개의 숫자 저장 가능 //a 범위: -128~127(256개) System.out.println(Byte.MIN_VALUE); // 상수 -128 short b; //2byte //경우의 수: 2의 16승 //정수 데이터 타입의 범위 계산식: -(경우의 수/2) 부터 (경우의 수/2)-1 까지 //-2^(n-1) ~ ((2^(n-1))-1)와 동일(바이트 수: n) System.out.println(Short.MAX_VALUE); //wrapper class가 가지는 상수 MAX_VALUE System.out.println(Short.MIN_VALUE); int c; System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); long d; d = 9223372036854775807L; //integer보다 큰 숫자는 뒤에 L 붙임 System.out.println(Long.MAX_VALUE); System.out.println(Long.MIN_VALUE); float f = 3.14F; //4byte f = (float)3.14; //명시적, 선언적 형 변환 double e = 3.14; //8byte //실수의 기본 데이터 타입 중에 java가 지원하는 것 System.out.println("Float.MAX_VALUE="+Float.MAX_VALUE); System.out.println("Float.MIN_VALUE="+Float.MIN_VALUE); System.out.println("Double.MAX_VALUE="+Double.MAX_VALUE); System.out.println("Double.MIN_VALUE="+Double.MIN_VALUE); //E는 무엇일까? 10의 승, E308 == 10의 308승 //실수 계산법 -> 컴퓨터가 실수를 계산하는 법이 따로 있다 //float -> 둥둥 떠 있다(소수점이) == 부동 소수점 -E와 연관돼있음 } } | cs |
/** <-HTML 문서 작성 주석
* 사용설명서 제작 (Web에서 동작)
**/
/* <-여러 줄 주석
* 정수란
* 숫자 중에서 소수점이 없는 것, 0과 마이너스를 포함
* 정수 자료형(데이터타입)의 종류
* byte, short, int(*), long <- 1,2,4,8씩 저장
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class L04Casting { public static void main(String[] args){ //괄호: 우체통, 매개변수: 우편물 double d = 100.001; int i = 100; i = (int)d; System.out.println(i); //소수점 사라지고 100이 출력됨 d = i; //50.0이 출력(casting 없이도) int c = 140; byte b = (byte)c; System.out.println(b); //byte 최대값 127이 될 듯 하나, -116이 출력됨 //저장 범위가 큰 쪽은 형 변환 안 해도 됨 //도식도: double > float > long > int > short > byte //double(실수), int(정수)가 주로 사용됨 } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class L05Boolean { public static void main(String[] args) { boolean flag = true; flag = false; //비교 연산자를 수행 후 나타난다 System.out.println(2<1); String a = "일"; //String은 기본 데이터 타입이자, 클래스이다. String b = "일"; //기본 데이터 타입은 데이터를 같은 주소에 저장해 공유할 수 있다. System.out.println(a==b); //True 출력 String c = new String("일"); System.out.println("a==c: "+(a==c)); //왜 false가 나오지? ==는 값("일")을 비교하는 게 아니라 주소값(메모리 위치)을 비교 //비교 연산자에는 어떤 것들이 있을까? System.out.println("a.equals(c): "+a.equals(c)); //equals는 값("일")을 비교한다. ==보다 안정적. } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class L06Char { public static void main(String[] args) { char c1 = 'A'; char c2 = 97; //아스키 코드 char c3 = '\u0061'; //유니코드 System.out.println("c1 :"+c1); System.out.println("c2 :"+c2); System.out.println("c3 :"+c3); char c4 = 'a'; int code = c4;//char 타입을 int 타입으로 형 변환시 아스키 코드 값(숫자)으로 변한다. System.out.println(code); //반복문 for for(int i =0; i<100; i++){ System.out.println((c1++)+","); } //for end } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class L07String { public static void main(String[] args) { System.out.println("Hello"); //5*2byte System.out.println("1234567890"); //10*2byte String a = "hello"; //기본 String b = "hello"; //기본 String c = new String("hello"); //참조 String d = new String("hello"); //참조) System.out.println("a==b: "+(a==b)); System.out.println("a==\"hello\": "+(a=="hello")); System.out.println("a==c: "+(a==c)); System.out.println("c==d: "+(c==d)); System.out.println("a.equals(c): "+a.equals(c)); String e = "Hello Java1 Lesson"; System.out.println(e.indexOf("h")); //-1은 존재하지 않는다. System.out.println(e.indexOf("H")); //외국은 0부터 시작 System.out.println(e.indexOf("a")); //indexOf는 가장 앞에 있는 문자 index(위치 번호)를 반환 System.out.println(e.substring(6,11)); //index 6~11번째까지 문자열을 반환 System.out.println(e.toUpperCase()); //전부 대문자로 출력 System.out.println(e.toLowerCase()); //전부 소문자로 출력 } } | cs |
//String이 특별한 이유?
//1. 저장 방식: char의 조합
//2. 비교 방식: == vs equals
//3. 기본이자 참조형 데이터 타입인 이유? 용량 절약할 땐 (주소값) 참조형 방식 사용
//클래스는 설계도, 이것을 갖고 생성자 호출(new)==객체 생성
<String 클래스의 메소드>
1) concat : 문자열 연결
2) substring : 문자열 자르기
3) length : 문자열 길이
4) toUpperCase : 대문자로 만들기
5) toLowerCase : 소문자로 만들기
6) charAt : 특정위치의 글자 찾기
7) indexOf : 특정문자열의 위치
8) equals : 문자열 비교
9) trim : 문자열 공백제거
10) replace : 특정문자 변경
11) repalceAll : 특정문자열 변경
<String의 단점?>
메모리 과소비 문제가 있으므로 StringBuilder, StringBuffer 클래스가 사용됨
(버퍼와 빌더는 비슷하나 빌더가 좀 더 빠름)
<StringBuilder 클래스의 메소드>
1) Append : 문자열 추가
2) insert : 특정 위치에 문자열 추가
3) delete : 문자열 삭제
4) deleteCharAt : 특정 문자 하나 삭제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class L08Array { String name; public L08Array(){} //default 생성자 -> 생성자 만들지 않으면 컴파일시 자동 생성 public L08Array(String name){ this.name = name; } public static void main(String[] args) { int[] odds = {1, 3, 5, 7, 9}; String[] weeks={"월", "화", "수", "목", "금", "토", "일"}; String week1 = "월", week2 = "화", week3 = "수", week4 = "목", week5 = "금", week6 = "토", week7 = "일"; System.out.println("오늘은 "+weeks[2]+"요일입니다."); System.out.println(weeks.length); //index(차례)와 length(길이)의 차이 try{ //오류 생기는 부분 System.out.println(weeks[8]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(e.toString());} //어떤 오류 잡아낼 경우, 다음 행으로 넘어간다. //컴파일시 발견되지 않는(심각한 오류) -> 오류 발생 시점에서 프로그램 종료 //컴파일시 생기는 오류 -> 문법상 오류 System.out.println("프로그램 종료"); L08Array[] array = {new L08Array("월"), new L08Array("화"), new L08Array("수"), new L08Array("목")}; //1. 배열은 dataType과 동일한 객체를 배열로 가진다. //2. 선언된 index 길이는 변하지 않는다. } } | cs |
'Programming > Java' 카테고리의 다른 글
Day 6(8/8) DoWhile, BreakLoop, Method, Instance (0) | 2016.08.08 |
---|---|
Day 5(8/5) For, While (0) | 2016.08.05 |
Day 4(8/4) IF, Switch (0) | 2016.08.04 |
Day 2(8/2) Variable & DataType (0) | 2016.08.02 |
Day 1(8/1) 클래스 생성 (0) | 2016.08.01 |