"int 자료형의 범위를 알아내는 것이 이번 문제의 정답이다.
int는 정수형이고 32비트 자료형이다."
public class Main {
public static void main(String[] args) {
int min, max;
min = Integer.MIN_VALUE;
max = Integer.MAX_VALUE;
System.out.printf(min + " " + max);
}
}
생성자 메서드: 객체생성과 동시에 변수를 원하는 값으로 세팅(초기화)해줌.
class 사람 { // 사람객체안에 이름과 나이를 넣을 공간을 만들거야
String 이름;
int 나이;
//생성자 메서드명은 클래스명과 똑같이 적고 접근제어자 void등등 적지 않음
// 실행되는 시점은 객체가 생성됨과 동시에 실행
사람() {
this.나이 = 22;
}
}
public class Main {
public static void main(String[] args) {
사람 a사람1 = new 사람("김철수"); // 객체안에 인자값을 세팅
System.out.printf("이름 : %s\n", a사람1.이름); // 김철수
System.out.printf("나이 : %d\n", a사람1.나이); // 22
}
}
class 사람 {
String 이름;
int 나이;
사람(String 이름) { // 생성자안에 매개변수세팅
this.이름 = 이름;
}
}
/*
출력결과:
이름 : 김철수
나이 : 0
*/
// 김철수는 나이값이 0이다 왜냐하면 김철수가 들어가있는 생성자메서드에 나이 값이 없음.
static int lastId; // static 변수
static { // static 블럭
lastId = 0;
}
ArrayList articles = new ArrayList();
ArrayLists클래스는 자바 library에 이미 존재함 -> import할 수 있음
Array List에 제약사항을 건다.
ArrayList <Array List>articles = new ArrayList<Array List>();
// <Array List> Array List만 들어갈 수 있음
ArrayList <Integer>articles = new ArrayList<Integer>();
// <Integer> 정수만 들어갈 수 있다는 뜻
// < > 요소의 타입을 지정해주는 공간이라고 생각하면 된다
List<Article> articles = new ArrayList<>(); // 생략버전
내가 뭘 모르는가? 내가 뭐가 약한가?
public : 모든 접근을 허용
protected : 같은 패키지(폴더)에 있는 객체와 상속관계의 객체들만 허용
default : 같은 패키지(폴더)에 있는 객체들만 허용
private : 현재 객체 내에서만 허용
클래스에 필드들은 대부분 private -> 관례이다
class 사람 {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
키워드
class 계산기 {
static double 나누기(int a, int b) {
int result;
try { // 수행 할 문장 (안전하지 않은 코드)
result = a / b;
} catch (ArithmeticException e) { // 예외처리할 거
System.out.println("이거 뭔가 잘못됐다"); // // 예외가 발생하여 이 문장이 수행된다
result = 0;
}
return result;
}
}
팁!
try {
result = a / b;
} catch (ArithmeticException e) {
System.err.println("이거 뭔가 잘못됐다 이거 못해 걍 0으로 처리할게" + e);
System.exit(0);
result = 0;
}
return result;
}
// System.out 대신 System.err사용하면 출력화면에 빨간색으로 표시 됨
// e라는 변수를 출력에 사용하면 뭐가 문젠지 알려줌
public class Main {
public static void main(String[] args) {
try {
int rs = 계산기.나누기(10, 0);
System.out.println(rs);
}catch (ArithmeticException e) {
System.err.println("0으로 못나눠");
}catch (Exception e) {
System.err.println("알 수 없는 에러");
}
}
}
class 계산기 {
static int 나누기(int a, int b) throws ArithmeticException{
int result;
result = a / b;
return result;
}
}