Java 54

Java) this 키워드 역할과 사용법

this 키워드란?: this는 현재 객체 자신을 참조하는 키워드이다.: 객체 내부에서 자신의 멤버 변수와 메서드를 명확히 구분하고 접근할 수 있게 한다.: 메서드의 첫 번째 숨겨진 매개변수로 설정되어 있으며, 항상 현재 객체를 가리킨다.멤버 변수와 매개변수 구분class MyClass{ void setNumber(int number){ this.number = number; }}this.number : 멤버 변수number : 매개변수객체 자신을 참조class MyClass{ void function(){ System.out.println(this); // 현재 객체의 참조값 출력 }}: this는 클래스 내부에서 현재 객체의 참조값을 나타낸다.메서드..

Java 2025.01.06

Java) 객체지향 프로그래밍 : 캡슐화(은닉성) 예제

캡슐화(은닉성)이란?: 외부(클래스 밖)와의 접근을 차단 또는 혀용하도록 하는 기능: 접근지정자를 사용하여 변수, 메소드의 접근을 차단 / 허용할 수 있다.접근지정자란?private(개인적인) : 외부 접근 차단public(대중적인) : 어디든지 접근 허용protected(보호) : 자식클래스에서만 접근 허용(상속관련). 외부 접근 차단멤버 변수 사용 예import cls.MyClass;public class MainClass { public static void main(String[] args) { MyClass mycls = new MyClass(); mycls.number = 1; // private으로 설정해서 접근할수가 없음 mycls.na..

Java 2025.01.06

Java) 객체지향 프로그래밍 : 클래스와 메소드 정리

class : 객체를 사용하기 위한 설계method : class에 소속되어 있는 함수 형식 :         클래스의 설계         class 클래스명{             변수 선언부             함수(method) 선언부         }         클래스를 생성         클래스명 객체(변수) = new 클래스명();         객체.함수()객체지향 프로그래밍 기본 예제public class MainClass { public static void main(String[] args) { MyClass cls = new MyClass(); // 객체(instance, object)를 선언 // stack heap ..

Java 2025.01.05

Java) 회원 관리 프로그램 : 파일 저장 및 읽기

조건 : 1. 파일명을 입력받는다.2. 파일에 회원 수를 입력 받는다.3. 파일로부터 모든 회원을 읽어 들여 String 배열에 저장한다.파일 쓰기Scanner sc = new Scanner(System.in);파일 이름 설정System.out.print("사용할 파일명을 입력하세요 : ");String fileName = sc.next();파일 생성File file = new File("C:\\IJ\\" + fileName + ".txt");if(file.createNewFile()){ System.out.print(fileName + "파일생성 성공!");}else { System.out.print(fileName + "파일명이 존재!");}: createNewFile() 함수를 사용파일 ..

Java 2025.01.05

Java) 파일 검색, 생성, 읽기/쓰기 설정, 삭제 정리

파일 + 디렉토리(폴더) 검색File fileDir = new File("c:\\");File fileList[] = fileDir.listFiles();for(int i = 0; i : fileDir에 검색할 폴더 위치 지정:ifFile(), isDirectory() 함수 사용파일생성File newFile = new File("C:\\IJ\\newfile.txt");try { if(newFile.createNewFile()){ System.out.println("파일 생성 성공"); }else{ System.out.println("파일 생성 실패"); }} catch (IOException e) { throw new RuntimeException(e);}:..

Java 2025.01.02

Java) 4가지 예외 클래스(Exception) 정리

NullPointException: null 객체 접근 시 발생// int array[] = new int[5];int array[] = null; // int i = 0;try { System.out.println(array.length);}catch(NullPointerException e){ System.out.println("배열이 할당되어 있지 않습니다.");}: 형식이나 사용법을 익히자.: null로 설정하면 i가 0이랑 같은거기 때문에 에러 발생 ArrayIndexOutOfBoundsException: 배열 범위 초과 시 발생 int array1[] = {1, 2, 3};try{ System.out.println(array1[3]);}catch(ArrayIndexOutOfBo..

Java 2025.01.02

Java) Exception 기초 & 실습

Exception : 예외 != 에러자주 사용하는 4가지 경우1. number -> format: 숫자를 입력해야 하는데 문자를 입력한 경우 2. array -> Index out of bound: 인덱스 번지를 벗어난 경우int array[] = new int[3]; // 0 ~ 2array[3] == 12; // 3번지는 없음 벗어남. 3. class -> class not found: class 못찾는 경우 4. file -> file not found: file을 못찾는 경우 형식 :        try{             처리코드1(예외가 나올 가능성)             처리코드2(예외가 나올 가능성)              }catch(예외클래스1){             1.예외가 나..

Java 2025.01.02