본문 바로가기

Java

자바의 신 ch3 & ch4

java파일을 컴파일 할 때, 파일명.java로 하지 않을 경우 발생하는 오류다. javac CarManager.java로 입력하니 문제 없이 실행되었다.

public class ProfilePrint {
  byte age;
  String name;
  boolean isMarried;
  
  public static void main(String[] args) {
    ProfilePrint profile=new ProfilePrint();
    profile.setAge((byte)25);
    profile.setName("Kim");
    profile.setMarried(false);
    System.out.println(profile.getAge());
    System.out.println(profile.getName());
    System.out.println(profile.isMarried());
    }
  
  public void setAge(byte age) {
    this.age=age;
    }
  public byte getAge() {
    return age;
    }
  public void setName(String name) {
    this.name=name;
    }
  public String getName() {
    return name;
    }
  public void setMarried(boolean flag){
    isMarried=flag;
    }
  public boolean isMarried() {
    return isMarried;
    }
  }

기능이 거의 없는 IDE를 사용하다보니, 기존에 거의 본적 없던 에러들이 많이 발생한다.

첫번째 에러는 객체를 생성하는 예약어와 클래스명 사이에 띄어쓰기를 하지 않아서 발생했다.

두번째 에러는 매개변수를 byte 타입으로 지정하였는데, byte타입으로 형변환을 해주지 않아서 발생했다. 범위가 넓은 쪽에서 좁은 쪽일 경우 (e.g. int -> byte) 형변환을 해주어야 한다. 반대로 byte를 int로 형변환 해야 할 경우 굳이 할 필요 없다.

'Java' 카테고리의 다른 글

자바의 신 ch7 & ch8  (0) 2022.06.09
자바의 신 ch5 & ch6  (0) 2022.06.08
자바의 신 ch1 & ch2  (0) 2022.06.06
LinkedList  (0) 2022.05.27
ArrayList  (0) 2022.05.27