System.out.print() // 화면에 정보를 출력하라는 의미입니다.
System.out.println()// 화면에 정보를 출력하고 줄 바꿈을 하라는 의미입니다.
System.out.printf() // 형식화한 문자열을 화면에 출력하라는 의미합
import java.util.Date;
public class FormatExample{
public static void main(String[] args){
int a = 12345;
System.out.printf("정수: [%d][%10d][%-10d][%010d]\n",a,a,a,a);
double b = 123.456;
System.out.printf("실수: [%f][%10.2f][%-10.4f][%010.4f]\n",b,b,b,b);
String c = "hello";
System.out.printf("문자: [%s][%10s][%-10s][%10S][%5.2s]\n",c,c,c,c,c);
System.out.printf("논리: [%b][%B]\n", true, false);
Date d = new Date();
System.out.printf("시간: [%tH:%tM:%tS]\n", d, d, d);
System.out.printf("날짜: [%1$ty-%1$tm-%1$td]", d); //1$ --> 첫번쨰 것을 매핑한다는 의미
}
}

System.in.read() - 바이트 단위 입력System.in은 표준 입력(키보드) 스트림이며 바이트 단위로 입력을 처리한다.System.in.read()는 1바이트만 읽기 때문에 영어, 숫자, 특수문자만 입력 가능하며, 한글은 처리할 수 없다.int keyCode = System.in.read();
System.out.println(keyCode); // 예: 'A' 입력 시 65 출력
System.out.printf("%c", keyCode); // A 출력
비유: 종이 한 장만 집을 수 있는 집게. 두꺼운 책(=한글)은 못 집는다.
System.in을 문자로 처리하게 하여 한글 입력도 가능하게 만든다.Reader reader = new InputStreamReader(System.in);
int keyCode = reader.read();
System.out.println(keyCode);
System.out.println((char)keyCode);
비유: 손가락 대신 집게를 바꾸면 책도 집을 수 있게 되는 것처럼, InputStreamReader는 더 큰 문자를 처리할 수 있게 도와준다.
System.in.read()를 통해 여러 바이트를 배열로 입력받고 문자열로 변환한다.byte[] buffer = new byte[100];
int readCount = System.in.read(buffer);
String inputData = new String(buffer, 0, readCount);
System.out.println(inputData);
주의: 입력한 문자열 끝에 줄바꿈 문자(엔터)가 포함되어 함께 출력될 수 있다.
BufferedReader는 문자열을 한 줄씩 입력받을 수 있게 해준다.Reader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
String line = in.readLine();
System.out.println(line);
in.close();
비유: 한 줄씩 뜯어지는 메모장처럼 한 줄 단위로 입력받는 방식.
Scanner는 다양한 형식의 데이터를 입력받을 수 있는 유용한 도구.Scanner scanner = new Scanner(System.in) 으로 생성한다.next(): 공백 전까지 문자열 입력nextLine(): 한 줄 전체 입력nextInt(), nextDouble(), nextBoolean() 등 다양한 타입 지원Scanner scanner = new Scanner(System.in);
String name = scanner.next();
int age = scanner.nextInt();
double weight = scanner.nextDouble();
next() vs nextLine() 주의사항nextInt()나 nextDouble() 같은 메서드는 엔터키를 소비하지 않는다.nextLine()을 쓰면 이전 줄의 남은 엔터가 그대로 입력되어 빈 문자열이 들어간다.Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt(); // 엔터는 안 소비됨
String name = scanner.nextLine(); // 바로 빈 문자열이 입력됨 (버그)
nextLine()으로 먼저 전체 줄을 받고 필요한 형으로 변환하는 방식 사용int age = Integer.parseInt(scanner.nextLine());
String name = scanner.nextLine();
hasNextLine() 활용)hasNextLine() 사용Scanner scanner = new Scanner(System.in);
StringBuilder lines = new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.trim().isEmpty()) break;
lines.append(line).append("\n");
}
System.out.println(lines.toString());
비유: 사용자가 "이제 그만" 하고 빈 줄을 입력할 때까지 메모장에 계속 적는 것과 같다.
| 입력 방식 | 처리 단위 | 한글 처리 | 줄 단위 입력 | 다양한 타입 지원 | 특징 |
|---|---|---|---|---|---|
System.in.read() | 바이트 | X | X | X | 가장 기본, 1바이트만 읽음 |
InputStreamReader | 문자 | O | X | X | 한글 가능 |
BufferedReader | 줄 | O | O | X | 성능 좋고 직관적 |
Scanner | 토큰 / 줄 | O | O | O | 가장 다용도, 형변환 주의 |
public class IncremintExample {
public static void main(String[] args){
int a = 5;
++a;
System.out.println("전위증가:" + a);
int b = 10;
b++;
System.out.println("후위증가: " + b);
}
}
// 전위 증가 : 6
// 후위 증가 : 11
public class IncrementExample2 {
public static void main (String[] args) {
int i = 1;
int j = i++;
System.out.println("후위 연산자");
System.out.println("i의 값은 " + i);
System.out.println("j의 값은 " + j);
int x = 1;
int y = ++x;
System.out.println("\n전위 연산자");
System.out.println("x의 값은 " + x);
System.out.println("y의 값은 " + y);
}
}
// i의 값은 2
// j의 값은 1
// x의 값은 2
// y의 값은 2
public class Sign0pExample{
public static void main(String[] args){
int a = 3;
int b = - -a;
System.out.println("a: " + a);
System.out.println("b: " + b);
int c = 3;
int d = --c;
System.out.println("c: " + c);
System.out.println("d: " + d);
int x = 3;
int y = -x + - --x + x--;
System.out.println("x: " + x);
System.out.println("y: " + y);
}
}
// a: 3 b:3 c:2 d:2 x:1 y: -3
public class Not0pExample {
public static void main(String[] args) {
byte binData = 0b0000_1000;
System.out.println(binData);
System.out.println(~binData);
System.out.println((byte)0b1111_0111);
boolean isTure = false;
System.out.println(!isTrue);
}
}
public class DivmodExample1 {
public static void main(String[] args) {
int number = 45;
int tensDigit = number / 10;
int unitsDigit = number % 10;
int sum = tensDigit + unitDigit;
System.out.println("각 자릿수의 합 : " + sum);
}
}
public class StringComparsionExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
String str3 = "world";
String str4 = "world";
System.out.println(str3 == str4);
System.out.println(str3.equals(str4));
str3 = str3.concat("ABC");
System.out.println(str3);
System.out.println(str3 == str4);
}
}
public class BitwiseExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte a= 5;
byte b = 3;
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
}
}
// 1
// 7
// 6
, <<는 채워지는 비트는 부호비트
오른쪽으로 비트 이동하는데 채워지는 비트는 무조권 0
int a = 192;
int b = a >> 3;
// a => 11000000
// b => 00011000
public class ShortCircuitExample {
public static void main (String[] args) {
int x=10, y=20;
System.out.println((x != 10) & (++y == 21)); // false
System.out.println("y: " + y); // y: 21
System.out.println((x == 10) | (++y == 21)); // true
System.out.println("y: " + y); // y: 22
int a=10, b=20;
System.out.println((a != 10) && (++b == 21)); // false
System.out.println("b: " + b); // b: 20
System.out.println((a == 10) || (++b == 21)); // true
System.out.println("b: " + b); // b: 20
}
}
public class AssignmentExample {
public static void main(String[] args){
int a = 5;
int b = 5;
a += 3;
b += 3;
System.out.println(a+ "\t" + b); // 8, 3
}
}
(조건식 ? 연산식1 : 연산식 2) → 조건식이 참이면 연산식1이 반환되고 거짓이면 연산식2가 반환됩니다.
public class ConditionalExample{
public static void main(String[] args){
int a= 5 - (int)(Math.random() *10); //0부터 1미만의 임의의 실수를 반환하고 10을 곱함
int abs = a >= 0 ? a: -a;
System.out.println(a + "의 절댓값 : " + abs);
System.out.println(a >= 0 ? "0보다 크거나 같음" : "0보다 작음");
}
}