[JAVA] 연산자 예시

이현경·2021년 4월 8일
0

JAVA

목록 보기
8/77

public class ArithmeticOperatorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int v1 = 5;
		int v2 = 2;
		
		int result1 = v1 + v2;
		System.out.println("result1 = " + result1);	// 7
		
		int result2 = v1 - v2;
		System.out.println("result2 = " + result2);	// 3
		
		int result3 = v1 * v2;
		System.out.println("result3 = " + result3);	// 10
		
		int result4 = v1 / v2;
		System.out.println("result4 = " + result4);	// 2
		
		int result5 = v1 % v2;
		System.out.println("result5 = " + result5);	// 1
		
		double result6 = (double)v1 / (double)v2;
		System.out.println("result6 = " + result6);	// 2.5
		
	}

}

public class AssignmentOperatorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int result = 0;
		result += 10;
		System.out.println("result = " + result);	// 10
		
		result -= 5;
		System.out.println("result = " + result);	// 5
		
		result *= 3;
		System.out.println("result = " + result);	// 15
		
		result /= 5;
		System.out.println("result = " + result);	// 3
		
		result %= 3;
		System.out.println("result = " + result);	// 0
		
	}

}

public class CharOperationExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		char c1 = 'A' + 1;
		char c2 = 'A';
		
		System.out.println("c1 : " + c1);	// B
		System.out.println("c2 : " + c2);	// A

	}

}

public class CheckOverflowExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			int result = safeAdd(2000000000, 2000000000);
			System.out.println(result);
		} catch(ArithmeticException e) {
			System.out.println("오버플로우가 발생하여 정확하게 계산할 수 없음");
		}
	}
	
	public static int safeAdd(int left, int right) {
		if ((right > 0)) {
			if (left > (Integer.MAX_VALUE - right)) {
				throw new ArithmeticException("오버플로우 발생");
			}
		} else {
			if (left < (Integer.MIN_VALUE - right)) {
				throw new ArithmeticException("오버플로우 발생");
			}
		}
		
		return left + right;
		
	}

}

public class CompareOperatorExample1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int num1 = 10;
		int num2 = 10;
		boolean result1 = (num1 == num2);
		boolean result2 = (num1 != num2);
		boolean result3 = (num1 <= num2);
		System.out.println("result1 = " + result1);	// true
		System.out.println("result2 = " + result2);	// false
		System.out.println("result3 = " + result3);	// true
		
		char char1 = 'A';
		char char2 = 'B';
		boolean result4 = (char1 < char2);
		System.out.println("result4 = " + result4);	// true
	}

}

public class CompareOperatorExample2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int v2 = 1;
		double v3 = 1.0;
		System.out.println(v2 == v3);	// true
		
		double v4 = 0.1;
		float v5 = 0.1f;
		System.out.println(v4 == v5);	// false
		System.out.println((float)v4 == v5);	// true
		System.out.println(v4 == (double)v5);	// false
		
	}

}

public class ConditionalOperationExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int score = 85;
		char grade = (score > 90) ? 'A' : ((score > 80) ? 'B' : 'C');
		System.out.println(score + "점은 " + grade + "등급 입니다.");
		// 85점은 B등급 입니다.
		
	}

}

public class DenyLogicOperatorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		boolean play = true;
		System.out.println(play);	// true
		
		play = !play;
		System.out.println(play);	// false
		
		play = !play;
		System.out.println(play);	// true
		
	}

}

public class IncresaseDecreaseOperaitorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int x = 10;
		int y = 10;
		int z;
		
		System.out.println("================");
		x++;	// 11
		++x;	// 12
		System.out.println("x = " + x);	// 12
		
		System.out.println("================");
		y--;	// 9
		--y;	// 8
		System.out.println("y = " + y);	// 8
		
		System.out.println("================");
		z = x++;	// 13
		System.out.println("z = " + z);	// 12
		System.out.println("x = " + x);	// 13
		
		System.out.println("================");
		z = ++x;	// 14
		System.out.println("z = " + z);	// 14
		System.out.println("x = " + x);	// 14
		
		System.out.println("================");
		z = ++x + y++;	// 15 + 8
		System.out.println("z = " + z);	// 23
		System.out.println("x = " + x);	// 15
		System.out.println("y = " + y);	// 9
		
	}

}

public class LogicalOperatorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int charCode = 'A';
		
		if ((charCode >= 65) & (charCode <= 90)) {
			System.out.println("대문자 이군요");
		}
		
		if ((charCode >= 97) && (charCode <= 122)) {
			System.out.println("소문자 이군요");
		}
		
		if (!(charCode < 48) && !(charCode > 57)) {
			System.out.println("0~9 숫자 이군요");
		}
		
		int value = 6;
		
		if ((value % 2 == 0) | (value % 3 == 0)) {
			System.out.println("2 또는 3의 배수 이군요");
		}
		
		if ((value % 2 == 0) || (value % 3 == 0)) {
			System.out.println("2 또는 3의 배수 이군요");
		}
		
	}

}

public class OverflowExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int x = 1000000;
		int y = 1000000;
		int z = x * y;
		System.out.println(z);	// overflow
		
	}

}

public class OverflowExample2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		long x = 1000000;
		long y = 1000000;
		long z = x * y;
		System.out.println(z);	// 1000000000000

	}

}

public class SignOperatorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int x = -100;
		int result1 = +x;
		int result2 = -x;
		System.out.println("result1 = " + result1);
		System.out.println("result2 = " + result2);
		
		short s = 100;
		int result3 = -s;
		System.out.println("result3 = " + result3);
		
	}

}

public class StringConcatExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String str1 = "JDK" + 6.0;	// JDK6.0
		String str2 = str1 + " 특징";	// JDK6.0 특징
		System.out.println(str2);	// JDK6.0 특징
		
		String str3 = "JDK" + 3 + 3.0;	// JDK33.0
		String str4 = 3 + 3.0 + "JDK";	// 6.0JDK >> 3 + 3.0 연산 먼저 실행
		System.out.println(str3);	// JDK33.0
		System.out.println(str4);	// 6.0JDK
		
	}

}
profile
25. 컴퓨터학과 졸업 / SQLD, 정보처리기사 취득

0개의 댓글