Boolean Datatype
true와 false로 이루어져있는 Boolean type
public class BooleanApp {
public static void main(String[] args) {
System.out.println("One");
System.out.println(1);
System.out.println(true);
System.out.println(false);
String foo = "Hello world";
// Stirng true = "Hello world"; reserved word
System.out.println(foo.contains("world"));
System.out.println(foo.contains("A"));
}
}
contains는 포함되어있으면 true 없으면 false
비교연산자
비교연산자는 왼쪽의 값과 오른쪽의 값을 비교할 때 쓰임
public class ComparisonOperatorApp {
public static void main(String[] args) {
System.out.println(1 > 1); // false
System.out.println(1 == 1); //true
System.out.println(1 < 1);
System.out.println(1 >= 1);
}
}
조건문
public class IfApp {
public static void main(String[] args) {
System.out.println("a");
if(false) {
System.out.println(1);
} else if(true) {
System.out.println(2);
} else {
System.out.println(3);
}
System.out.println("b");
}
}
조건문 응용1
public class AuthApp {
public static void main(String[] args) {
String id = "A";
String inputId = args[0];
System.out.println("Hi.");
// if(inputId == id) {
if(inputId.equals(id)) {
System.out.println("Master!");
} else {
System.out.println("Who are you?");
}
}
}
조건문 응용2
public class AuthApp {
public static void main(String[] args) {
String id = "A";
String inputId = args[0];
String pass = "1111";
String inputPass = args[1];
System.out.println("Hi.");
// if(inputId == id) {
// if(inputId.equals(id)) {
// if(inputPass.equals(pass)) {
// System.out.println("Master!");
// } else {
// System.out.println("Wrong password");
// }
// } else {
// System.out.println("Who are you?");
// }
if(inputId.equals(id) && inputPass.equals(pass)) {
System.out.println("Master!");
} else {
System.out.println("Who are you?");
}
}
}