JAVA_02_타입,타입변환,연산자

hyeong taek jo·2023년 7월 3일

JAVA

목록 보기
2/39

📌정수타입

byte : 1 바이트
short : 2 바이트
char : 2 바이트
int : 4 바이트
long : 8바이트

📌실수타입

float : 4바이트
double : 8바이트

📌char타입

작은 따옴표로 (‘’)로 감싸는 것을 문자 리터럴이라고 한다. 문자 리터럴은 유니코드로 저장된다.

📌 자동 타입변환

  • 큰 타입에서 작은 타입으로는 자동 변환이 안되어 강제로 변한해주어야 한다.
  • 저장하려는 값 앞에 큰 타입을 적어주면 된다.
  • 강제로 변환하기 때문에 온전한 값이 저장되지 않을 수 있다.
  • ex : int x = 10;
    byte k = (byte) x;

📌 문자열 자동 타입변환

  • “문자열” + 숫자 → “문자열”
  • 숫자 + “문자열” → “문자열”
    		int value = 10 + 2 + 8;
    		System.out.println("value: " + value);		
            //결합 연산
    		String str1 = 10 + 2 + "8";
    		System.out.println("str1: " + str1); // 피연산자 중 하나가 문자열일 경우에는 나머지 피연산자도 문자열로 변함
                                                 // 값이 128인 이유는 앞에서부터 연산되는데 앞에는 숫자 + 슷자라서 12로 더해지고 숫자 + 문자열이라서 문자열로 됨
    		String str2 = 10 + "2" + 8;
    		System.out.println("str2: " + str2); // 둘 중 하나라도 문자열이면 나머지도 문자열로 변경됨
    		String str3 = "10" + 2 + 8;
    		System.out.println("str3: " + str3); // 둘 중 하나라도 문자열이면 나머지도 문자열로 변경됨
    		String str4 = "10" + (2 + 8);
    		System.out.println("str4: " + str4); // 둘 중 하나라도 문자열이면 나머지도 문자열로 변경됨
            [str2 : 1028]
            [str3 : 1028]
            [str4 : 1010]

📌문자열을 기본타입으로 강제 타입변환

  • Integer.parse을 사용한다.
  • 강제로 변환하더라도 변환하려는 타입의 형식은 맞아야 한다. “A1”을 int로 강제 변환은 불가능하다.
		//int value11    = Integer.parseInt("A1");
		double value2   = Double.parseDouble("3.14");
		boolean value3  = Boolean.parseBoolean("true");		
		System.out.println("value1: " + value1);
		//System.out.println("value11: " + value11);
		System.out.println("value2: " + value2);
		System.out.println("value3: " + value3);```
[value : 10]
[value : 3.14]
[value : true]

📌기본타입을 문자열로 강제변환

  • valueOf를 사용한다.
		int valueA      = 10;
		double valueB   = 3.14;
		boolean valueC  = true;	
		String str1 = String.valueOf(valueA);
		String str2 = String.valueOf(valueB);
		String str3 = String.valueOf(valueC);
		System.out.println("str1: " + str1);
		System.out.println("str2: " + str2);
		System.out.println("str3: " + str3);
[str1 : 10]
[str2 : 3.14]
[str3 : true]

📌키보드에 입력된 내용을 변수에 저장

  • System.in.read( ), Scanner 등이 있는데 잘 안쓴다

📌삼항 연산자

int score = 85;
		char grade = (score > 90) ? 'A' : ((score > 80) ? 'B' : 'C');
		System.out.println(score + "점은 " + grade + "등급입니다.");

["85점은 B등급입니다."]

📌char연산자

char c1 = 'A' + 1;
		char c2 = 'A';
		//char c3 = c2 + 1; -> c2는 char 이고, 1 은 int형 인데 int가 더 크므로
		//int로 변한다. 그래서 c3를 int로 받으면 된다.	
		System.out.println("c1 : "+ c1);
		System.out.println("c2 : "+ c2);

[c1 : B],
[c2 : A]

📌증감 연산자

int x = 10;
int y = 10;
int z;
int aa, bb;
-
System.out.println("--------------------------");
aa = x++; //출력하고 나서 +1 // 1. aa = x , 2. x++
bb = ++x; //출력하기 전에 +1 // 1. ++x , 2. bb= x
-
System.out.println("aa=" + aa);
System.out.println("bb=" + bb);
System.out.println("x=" + x);
-		
System.out.println("--------------------------");
y--;
--y;
System.out.println("y=" + y);
- -	
System.out.println("---------------------------");
z = ++x + y++;  // 1. ++x -> 13, 2. y = 8, 3. z = 21, 4. y++
System.out.println("z=" + z);
System.out.println("x=" + x);
System.out.println("y=" + y);

[aa = 10],
[bb = 12],
[x = 12],
----------,
[y = 8],
----------,
[z = 21],
[x = 13],
[y = 9]

형식화된 문자열
정수
profile
마포구 주민

0개의 댓글