특수 문자와 서식 문자
특수 문자
- 일반 문자가 아닌 특수한 목적으로 사용되는 문자
\t
\n
\'
\"
\\
//
/* */
package testPJT2;
public class MainClass {
public static void main(String[] args) {
System.out.println("Good morning");
System.out.println("Good\tmorning");
System.out.println("Good\nmorning");
System.out.println("Good\'morning\'");
System.out.println("Good\"morning\"");
System.out.println("Good\\morning\\");
}
}
//결과
Good morning
Good morning
Good
morning
Good'morning'
Good"morning"
Good\morning\
서식 문자
- 어떠한 식을 나타냄
printf()
메서드 이용함
- f는 format을 의미
- 문자열 끝에 개행문자를 포함하고 있지 않음
println()
의 경우 자동으로 개행문자를 포함하는 것과 다름
%d
%o
%x
%c
%s
%f
package testPJT2;
public class MainClass {
public static void main(String[] args) {
System.out.printf("you are %dyears old\n", 10);
int num1 = 20;
System.out.printf("i am %dyears old\n", num1);
System.out.printf("%s is same with %s\n", "aa", "AA");
}
}
//결과
you are 10years old
i am 20years old
aa is same with AA
서식 문자의 정렬
System.out.printf("%5d", 123);
//결과
123
%d
사이에 숫자를 넣으면 해당 수만큼 자리를 미리 확보해놓고 오른쪽정렬시킴
서식 문자의 소수점 제한 기능
System.out.printf("%.3f", 0.1234567);
//결과
0.123
%f
사이에 .표시할 소수점 자리수
를 넣으면 해당 수만큼만 출력함
- 출력하는 float값 이상을 넣을경우 0을 채움