#include <stdio.h>
int main()
{
int num1 = 0;
int size;
size = sizeof num1; // 변수 num1의 자료형 크기를 구함
printf("num1의 크기: %d\n", size);
return 0;
}
// 출력
// num1의 크기: 4
#define _CRT_SECURE_NO_WARNINGS // scanf 보안 경고로 인한 컴파일 에러 방지
#include <stdio.h>
int main()
{
int num1;
scanf("%d", &num1); // 값을 입력받음
// switch의 case에서 break 삭제
switch (num1)
{
case 1: // 1일 때는 아래 case 2, default가 모두 실행됨
printf("1입니다.\n");
case 2: // 2일 때는 아래 default까지 실행됨
printf("2입니다.\n");
default:
printf("default\n");
}
return 0;
}
숫자인지 아닌지 판단하는 함수
#include <stdio.h>
#include <ctype.h>
int main()
{
char arr[13] = "ABCDEF123456";
printf("arr : %s \n\n", arr);
for (int i = 0; i < 12; i++)
{
printf("arr[%d] = %c isdigit: %d \n",i,arr[i], isdigit(arr[i]));
}
return 0;
}
ex)
class SuperObject {
public void paint(){
draw();
}
public void draw(){
draw();
System.out.println("Super Object");
}
}
class SubObject extends SuperObject {
public void paint(){
super.draw();
}
public void draw(){
System.out.println("Sub Object");
}
}
public class Test{
public static void main(String[] args){
SuperObject a = new SubObject();
a.paint();
}
}
클래스 SubObject를 정의하고 부모 클래스로 SuperObject를 지정하면 SuperObject에 속한 변수와 메서드를 상속받습니다.
자식 클래스 생성자로 인스턴스를 생성할 때 자료형을 부모 클래스로 지정하면 생성된 인스턴스는 부모 클래스로 묵시적 클래스 형 변환이 됩니다. 부모와 자식 클래스간 같은 메서드가 존재하면 호출되는 메서드는 생성된 인스턴스에 따라 결정됩니다.
a.paint()는 클래스 형 변환을 수행하였고 print()메서드가 자식 클래스에서 재정의를 통해 오버라이딩 된 메서드이므로 자식 클래스의 paint 메서드가 수행됩니다.
부모 클래스를 호출하는 super를 사용했으므로 부모 클래스의 draw() 메서드를 수행합니다.
부모 클래스 draw()에서 처음에 클래스 형 변환을 수행하였고 draw()메서드가 자식 클래스에서 재정의를 통해 오버라이딩 된 메서드이므로 자식 클래스의 draw()메서드를 수행합니다.
자식 메서드 draw를 수행하면서 'Sub Object가 수행되고' 다시 부모 draw()로 돌아가 나머지 'Super Object'를 수행합니다.
SuperObject a = new SubObject();
public class Main {
public static void main(String[] args) {
String str1 = "Programming";
String str2 = "Programming";
String str3 = new String("Programming");
System.out.println(str1 == str2); // ①
System.out.println(str1 == str3); // ②
System.out.println(str1.equals(str3)); // ③
System.out.print(str2.equals(str3)); // ④
}
}
//결과
true
false
true
true
1️⃣ String str1 = "Programming";
문자열 리터럴 "Programming"을 String Constant Pool(문자열 상수 풀)에 저장.
str1이 "Programming"을 가리킴.
2️⃣ String str2 = "Programming";
동일한 문자열 "Programming"이 이미 String Pool에 존재.
str2는 str1과 동일한 "Programming"을 참조.
3️⃣ String str3 = new String("Programming");
new String("Programming")은 Heap 영역에 새로운 문자열 객체를 생성.
str3은 String Pool의 "Programming"을 참조하지 않고, 새로 생성된 객체를 참조
해설
따라서 str1, str2는 동일한 Programming을 참조 하지만, 생성자를 통한 str3은 힙영역에 저장되기 때문에 str1, str2와 다른 주소를 갖고 있다.
리스트 생성 방법
a = ['a', 'b', 'c']
세트 생성 방법
a = {'a', 'b', 'c'}
리스트
a = [3, 1.234, 'ABC']
세트
리스트 관련 주요 메서드
Ex)
[1, 2, 3].append(4) -> [1, 2, 3, 4]
ex)
[10, 11, 12].pop(1) -> 11 출력 -> [10, 12]
ex)
[10, 11, 12].index(2) -> 2
ex)
[1, 0, 1, 1, 0].count(1) -> 3
ex)
['a', 'b'].extend(['c', 'd']) -> ['a', 'b', 'c', 'd']
ex)
[10, 11, 12].reverse() -> [12, 11, 10]
ex)
[2, 1, 3].sort() -> [1, 2, 3]
[2, 1, 3].sort(reverse = True) -> [3, 2, 1]
ex)
a = [1,2,3]
b = a.copy()
세트 관련 주요 메서드
ex)
{10, 11, 12}.pop() -> 10 출력 -> {11, 12}
ex)
{10, 11, 12}.add(13) -> {10, 11, 12, 13}
ex)
{'a', 'b', 'c'}.update({'c', 'd'}) -> {'a', 'b', 'c', 'd'}
ex)
{10, 11, 12}.remove(11) -> {10, 12}
튜플 생성 방법
tuple1 = (1, 'b', 'c')
tuple2 = tuple(['a', 'b'])
튜플
ex)
tuple = (1,)
range(최종값)
range(초기값, 최종값)
range(초기값, 최종값, 증가값)
객체명[초기위치:최종위치]
객체명[초기위치:최종위치:증가값]
객체명[:] 또는 객체명[::]
객체명[초기위치:]
객체명[:최종위치]
객체명[::증가값]
A='abcd'
print(A.upper()) #ABCD
print(A.capitalize()) #Abcd
print(A.title()) #Abcd
B='a2b3c4'
print(B.upper()) #A2B3C4
print(B.capitalize()) #A2b3c4
print(B.title()) #A2B3C4
C="abc-def efg"
print(C.upper()) #ABC-DEF EFG
print(C.capitalize()) #Abc-def efg
print(C.title()) #Abc-Def Efg
str = "findletter"
print(str.find("d"))
#
#결과
3
find 함수 첫번째 인자- 찾을 문자열 혹은 찾을 문자
find 함수 두번째 인자 (생략가능)- 문자를 찾을때 어디서 부터 찾을지 시작 index. 생략시 0
find 함수 세번째인자 (생략가능)- 문자를 찾을때 어디 까지 찾을지 끝 index, 생략시 문자열 맨 마지막 index
만약 찾는 단어가 없다면 -1 출력
str = "findletter"
print(str.find("s"))
#
#결과
-1
중복된 단어가 있다면 가장 첫번째 인덱스를 출력
str = "findletter"
print(str.find("t"))
#
#결과
6 # 7 인덱스에도 t가 있으나 6만 확인 가능
str = "findletter"
print("d" in str)
print("s" in str)
#
#결과
True
False
'A' = 65
'a' = 97
알아두기 혹시 모르니