본문 바로가기
Tech-Java

기타등등

by redcrow 2017. 4. 2.

1. Autoboxing

http://blog.naver.com/jiruchi/10093223469

http://knight76.tistory.com/entry/%EC%9E%90%EB%B0%94-autoboxing-%EC%8B%A4%EC%88%98-%EC%8B%A4-%EC%82%AC%EB%A1%80



2. Console , Scanner


1) Console

Java 6 부터 콘솔에서 입력된 문자열을 처리하기 위해 클래스. 이클립스에는 System.console()은 null을 리턴하므로 명령 프롬프트에서 실행필요.


public String readLine()

Reads a single line of text from the console

public char[] readPassword()

Reads a password or passphrase from the console with echoing disabled




import java.io.Console;

public class Test {

public static void main(String[] args) {

String line;

Console c = System.console();

if ( (line = c.readLine()) != null)

     System.out.println(line);

}

}

=> 컴파일에러는 발생하지 않지만 콘솔이 없는 환경(이클립스등)에서는 실행시 NullPointerException 발생

     아래처럼 Console이 null인지 체크할 필요가 있다


public class Test {

public static void main(String[] args) {

String line;

Console c = System.console();

if ((c != null ) && (line = c.readLine()) != null)

    System.out.println(line);

}

}



2) Scanner


A simple text scanner which can parse primitive types and strings using regular expressions.



boolean nextBoolean() : Scans the next token of the input into a boolean value and returns that value.

byte nextByte() : Scans the next token of the input as a byte.

byte nextByte(int radix) : Scans the next token of the input as a byte.

double nextDouble() : Scans the next token of the input as a double.

float nextFloat() : Scans the next token of the input as a float.

int nextInt() : Scans the next token of the input as an int.

int nextInt(int radix) : Scans the next token of the input as an int.

String nextLine() : Advances this scanner past the current line and returns the input that was skipped.

long nextLong() : Scans the next token of the input as a long.

long nextLong(int radix) : Scans the next token of the input as a long.

short nextShort() : Scans the next token of the input as a short.

short nextShort(int radix) : Scans the next token of the input as a short.


Scanner in = new Scanner(System.in);


int i = in.nextInt();

String s = in.next();



3. 자바 exception에는 checked exception과 unchecked exception 두 가지가 있다.


[checked exception]

- 메소드 시그니쳐에서 명시되어 있어야 하고 그 예외를 발생시킬 수 있는 메소드를 호출하는 쪽에서 예외에 대한 처리를 필수적으로 해줘야 한다.(try/catch or throw)

- Exception 클래스를 상속받아 만들면 checked exception이 된다.


[unchecked exception]

- 메소드 시그니쳐에 명시되어 있을 필요는 없고 그 예외를 발생시킬 수 있는 메소드를 호출하는 쪽에서 예외에 대한 처리를 필수적으로 해줄 필요는 없다.

- RuntimeException 클래스를 상속받아 만들면 unchecked exception이 된다.

- unchecked exception은 메소드 시그니쳐에 명시하지 않아도 throws 될 수 있다고 보면 된다.


댓글