정리/Java
자바의 정석 15~16강 문자, 문자열 리터럴, 문자열 결합
민발자
2023. 4. 26. 02:48
728x90
ch2-7,8 문자, 문자열 리터럴, 문자열 결합
1.문자와 문자열
문자 char ''
문자열 String ""
String은 클래스라 new연산자 이용해 생성 후 사용하는게 맞지만
자주 사용하는 클래스라 기본형처럼 new연산자없이 사용 가능
String은 빈문자열, 문자 한 개, 문자열 모두 가능
2. 문자열 결합
public static void main(String[] args) {
char ch = 'A';
//char ch = 'AB'; 한글자만 가능
//A의 문자코드가 저장
int i = 'A';
System.out.println(i);
//문자열 결합
String s1 = "A" + "B";
System.out.println(s1);
//int인 7을 문자열로 변경
//문자열 + 숫자 -> 문자열 + 문자열 -> 문자열로 결합되어 변경된다
String s2 = "" + 7;
System.out.println(s2);
//위치에 따라 결과값이 달라짐
String s3 = ""+7+7;
System.out.println(s3);
String s4 = 7+7+"";
System.out.println(s4);
}
ch2-9 두 변수의 값 바꾸기
public static void main(String[] args) {
int x = 10;
int y = 20;
int tmp = 0;
tmp = x; //1. x값을 tmp에 저장
x = y; //2. y의 값을 x에 저장
y = tmp; //3. tmp의 값을 y에 저장
System.out.println("x " + x);
System.out.println("y " + y);
}
728x90