dev.lang/Java
190613
seuhyang
2019. 6. 13. 23:32
Chap02_day3_190613
CharToCode.java
public class CharToCode {
public static void main(String[] args) {
char ch = 'A'; // char ch = 65;;
int code = (int)ch; // ch에 저장된 값을 int타입으로 변환하여 저장한다. (int)를 쓰지 않아도 저장이 된다.
System.out.printf("%c = %d(%#X or %#x) %n", ch, code, code, code);
char hch = '가'; // char hch = 0xAC00;
// char hch = 44032;
// char hch = 0xAC00;
// char hch = '\uAC00'; u > 유니코드 ,
System.out.printf("%c = %d(%#X or %#x) %n", hch, (int)hch, (int)hch, (int)hch);
// char hch 에 대한 decimal 변환이 없이 때문에 (int)를 붙여준다.
String sep = System.getProperty("line.separator"); // 줄에 관련된 속성을 가져와서 sep 에 대입
System.out.println(sep); // 출력된 \n and \r
for(char c : sep.toCharArray()) { // 대입된 sep 를 c에 넣어서 ㅇㅇ??
System.out.println((int)c); // ASCII code 13 = line feed , 10 = carriage return
}
}
SpecialCharEx.java
public class SpecialCharEx {
public static void main(String[] args) {
System.out.println('\''); // "'"처럼 할 수 없다.
System.out.println("abc\t123\b456");// \b (backspace)에 의해 3이 지워진다? 왜 안지워짐?
System.out.println('\n'); // 개행(new line) 문자 출력하고 개행
System.out.println("\"hello\""); // 큰 따옴표를 출력
System.out.println("c:\\"); // \를 출력 하기위해선 \\
}
}
OverflowEx.java
public class OverflowEx {
public static void main(String[] args) {
short sMin = Short.MIN_VALUE; //-32768
short sMax = 32767; //Short.MAX_VALUE
char cMin = 0; // MIN_VALUE
char cMax = Character.MAX_VALUE;
System.out.println("sMin : " + sMin);
System.out.println("sMax : " + sMax);
System.out.println("cMin : " + (int)cMin); // 문자열 을 정수로 보여줌
System.out.println("CMax : " + (int)cMax);
System.out.printf("overflow \n");
System.out.println("sMin - 1 : " + (short)(sMin-1)); // (-32768) + 1 // 이항 연산시 'int'로 타입 변경
System.out.println("sMax + 1 : " + (short)(sMax+1)); // 32767 - 1
System.out.println("cMin - 1 : " + (int)--cMin); // --0 => 66535
System.out.println("cMax + 1 : " + (int)++cMax); // ++66535 => 0
// System.out.println("sMin : " + sMin + "\n" + "sMin-1 : " + sMin-1); 안됨
System.out.println(3.14); // 부동소수점의 오차?
System.out.println(3.14 + 1); // 3.14의 실수부인 0.14가 2진수로 0으로 떨어지는 값이 나오지 않기 떄문에
// 오차(근사치)가 나온다. (= 2진화를 할 수 없다)
System.out.println(0.25); // 0으로 떨어지는 값이 나오는 수치는 정확히 값이 나온다.
System.out.println(0.25 + 1); //
}
}
CastingEx1.java
public class CastingEx1 {
public static void main(String[] args) {
double d =85.4;
int score = (int)d; // 실수 d 를 int로 형변환(Casting) 해서 d 에 저장
// 명시적 형변환
System.out.println("score : " + score); // 85.4를 int로 형변환 해서 저장할 경우
// int, 실수를 제외정수부분만 저장
System.out.println("d : " + d);
// 묵시적 형변환 (자동형변환)
float f = score;// int type < float type
//float f = d -> type mismatch , double type > float type
int i = 256;
System.out.println(i);
System.out.println((byte)i); // 256은 byte에서 제자리로 돌아와서 0
}
}
Chap03_day1_190613
Operator1.java
public class Operator1 {
public static void main(String[] args) {
// int i=5;
// i++; // i=i+1;
// System.out.println(i);
//
// i=5; // 결과비교 를 위한 i 값 변경
// ++i;
// System.out.println(i);
int i = 10; //단항 증감 , 후위형(suffix)
System.out.println(i);
i++; // 단항 증가 ++
System.out.println(i);
i--; // 단항 감소 --
System.out.println(i);
// i 10
++i; // 전위형(prefix)
System.out.println(i);
// 전위형 : 값이 참조되기 직전에 반영
// 후위형 : 값이 참조된 이후에 반영
System.out.println(++i);
System.out.println(i++);
System.out.println("i = "+ i);
i = i++ + ++i; // 13 출력후 증가, 출력전 증가 후 15 출력 => i에 28 대입
System.out.println(i); // 증감연산자는 한줄에 하나쓰는 것이 매너
}
}
OperatorEx2.java
public class OperatorEx2 {
public static void main(String[] args) {
int i = 5, j = 0;
j = i++; // 2개의 연산자 =, ++ . 단항연산자의 우선순위는 높다.
// 후위형이기 때문에 i값을 먼저 j에 대입해 주고 ++가 실행된다.
System.out.println("j=i++; 실행 후, i = " + i +", j = " + j);
i = 5;
j = 0;
j = ++i; // 전위형에 있는 ++ 먼저 실행하고 i 를 j에 대입한다.
System.out.println("j=i++; 실행 후, i = " + i +", j = " + j);
//전위형 : 값이 참조되기 직전에 반영
//후위형 : 값이 참조된 이후에 반영
//부호 연산자 - * - = + / - * + = - /
System.out.println(-i);
System.out.println(+i);
System.out.println(-(-i));
}
}
OperatorEx3.java
public class OperatorEx3 {
public static void main(String[] args) {
int i = 5, j = 5;
System.out.println(i++);
System.out.println(++j);
System.out.println("i = " + i + ", j = " +j);
}
}
Operator2.java
package day1_190613;
public class Operator2 {
public static void main(String[] args) {
int kor = 91;
int eng = 82;
int mat = 83;
int total = kor + eng + mat;
double avg = total * 100/ 3 / 100d; //마지막에 double 타입 접미사를 붙여줌으로서 소수점 자리를 줄임
System.out.println("총점 : " + total);
System.out.println("정수 평균 : " + (int)avg);
System.out.println("실수 평균 : " + avg);
}
}
OeratorEx5.java
public class OperatorEx5 {
public static void main(String[] args) {
int a = 10;
int b = 4;
System.out.printf("%d + %d = %d %n", a, b, a+b);
System.out.printf("%d - %d = %d %n", a, b, a-b);
System.out.printf("%d * %d = %d %n", a, b, a*b);
System.out.printf("%d / %d = %d %n", a, b, a/b); // 실수 부분을 뺀 정수 부분만 출력함
System.out.printf("%d / %.2f = %.2f %n", a, (float)b, a/(float)b);
}
}