== vs equals

 

비교연산자 ==는 변수의 값을 가리키는 곳의 위치가 같은지를 물어보는 것이고

equals는 변수 내부의 내용이 같은가를 묻는 것

 

원시 데이터 타입에는 7가지 boolean, int, double, short, long, float, char

 

원시 데이터 타입이 아닌 data type에는 String, Array, Date, File 등등이 있다.

 

즉 원시데이터 타입들은 동등비교연산자 == 를 쓰면 되고

 

원시데이터 타입이 아닌 것을 쓸 때에는 .equals()를 사용하면 된다

 

반복문

public class LoopApp {

	public static void main(String[] args) {

		System.out.println(1);
		System.out.println("=== while ===");
		
		int i = 0;
		while(i<3) {
			System.out.println(2);
			System.out.println(3);
			i++;
		}
		
		System.out.println("=== for ===");
		for(int j=0;j<3;j++) {
			System.out.println(2);
			System.out.println(3);
		}
		System.out.println(4);

	}

}

종합 응용 1

public class AuthApp3 {

	public static void main(String[] args) {

		String[] users = {"A", "B", "C"};
		String inputID = args[0];
		
		boolean isLogined = false;
		for(int i=0;i<users.length;i++) {
			String currentId = users[i];
			if(currentId.equals(inputID)) {
				isLogined = true;
				break;
			}
		}
		System.out.println("Hi, ");
		if(isLogined) {
			System.out.println("Master!!");
		} else {
			System.out.println("Who are you?");
		}
	}

}

 

종합 응용2

public class AuthApp3 {

	public static void main(String[] args) {

//		String[] users = {"A", "B", "C"};
		String[][] users = {
				{"A", "1111"},
				{"B", "2222"},
				{"C", "3333"}
		};
		
		String inputID = args[0];
		String inputPass = args[1];
		
		boolean isLogined = false;
		for(int i=0;i<users.length;i++) {
			String[] current = users[i];
			if(
					current[0].equals(inputID) &&
					current[1].equals(inputPass)
			) {
				isLogined = true;
				break;
			}
		}
		System.out.println("Hi, ");
		if(isLogined) {
			System.out.println("Master!!");
		} else {
			System.out.println("Who are you?");
		}
	}

}

'프로그래밍 언어 > JAVA' 카테고리의 다른 글

공부(12)  (0) 2021.09.24
공부(11)  (0) 2021.09.24
공부(9)  (0) 2021.09.24
공부(8)  (0) 2021.09.24
공부(7)  (0) 2021.09.23

+ Recent posts