2025년 6월 30일 월요일(24일차)

Jeonghoon·2025년 6월 30일

jeonghoon's Study

목록 보기
26/128

☕ Java 기초 총정리 📘


🧠 [ 자료형 기본 타입 (Primitive Data Types) ]

구분타입크기범위 / 설명비고
🔢 정수형byte1byte-128 ~ +127
short2byte약 ±30,000
int4byte약 ±21억정수 기본형
long8byte±21억 이상숫자 뒤에 L 또는 l
🌊 실수형float4byte소수점 8자리 표현숫자 뒤에 F 또는 f
double8byte소수점 17자리 표현실수 기본형
⚙️ 논리형boolean1bytetrue / false
🔤 문자형char2byte'문자 1개'
💬 문자열String클래스"문자 여러개"클래스 타입

🖨️ [ 출력 함수 ]

함수설명
System.out.print()줄바꿈 없는 출력
System.out.println()출력 후 줄바꿈
System.out.printf()형식에 맞춰 출력 (%s, %c, %d, %f)

📏 형식 지정자 예시

형식설명
%자릿수d지정한 자릿수만큼 확보 (공백 채움, 우측 정렬)
%-자릿수d지정한 자릿수만큼 확보 (공백 채움, 좌측 정렬)
%0자릿수d빈 공간을 0으로 채움
%전체자릿수.소수점자릿수f소수점 포함한 자릿수 제어

🔤 [ 이스케이프 문자 ]

문자의미
\n줄바꿈
\t들여쓰기
\\백슬래시 출력
\'작은따옴표 출력
\"큰따옴표 출력

☕ [ 변수의 타입변환 (Type Casting) ]

🎯 다형성(Polymorphism) : 하나의 자료가 여러 타입으로 변환될 수 있음

🔹 자동 타입변환 (묵시적)

변환 방향설명
byte → short/char → int → long → float → double작은 메모리 → 큰 메모리 자동 변환
byte b = 10;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
System.out.println(d); // 10.0

🔸 강제 타입변환 (명시적)

변환 방향설명
double → float → long → int → short/char → byte큰 메모리 → 작은 메모리, 손실 가능
double d = 3.14;
float f = (float)d;
long l = (long)f;
System.out.println(l); // 3 (소수 손실)

⚙️ 연산 타입변환

연산 예시결과 타입
byte + byteint
byte + shortint
int + longlong
int + floatfloat
int + doubledouble
int result = 10 + 3.14f; // float으로 자동 변환

⌨️ [ 입력 함수 (Scanner) ]

메서드반환 타입설명
.next()String문자열 입력 (공백 불가능)
.nextLine()String문자열 입력 (공백 가능)
.nextByte()byte정수 입력
.nextShort()short정수 입력
.nextInt()int정수 입력
.nextLong()long정수 입력
.nextFloat()float실수 입력
.nextDouble()double실수 입력
.nextBoolean()booleantrue/false 입력
.nextChar()❌ 없음대신 scan.next().charAt(0) 사용
Scanner scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
String name = scan.nextLine();
System.out.println("안녕하세요, " + name + "님!");

➕ [ 연산자 정리 ]

구분연산자설명
산술+, -, *, /, %사칙연산
연결+문자열 연결
비교>, <, >=, <=, ==, !=논리값 반환
논리&&,
증감++, --1씩 증가/감소
복합대입+=, -=, *=, /=, %=연산 후 대입
삼항조건 ? 참 : 거짓조건문 대체

🧮 예시

int x = 10, y = -3;
System.out.println(x + y);   // 7
System.out.println(x - y);   // 13
System.out.println(x / y);   // -3
System.out.println(x % y);   // 1

💬 문자열 vs 리터럴 비교

구분비교 방식예시결과
문자열 비교.equals()"10".equals("10")true
리터럴 비교==10 == 10true
잘못된 비교"10" == 10❌ 오류 발생

0개의 댓글