자바의 기본 연산자의 사용법을 이해 할 수 있는 4가지 예제

1. 기본 연산자 사용 예제

public class Arithmetic {
	public static void main(String args[]) {
		int a = 5, b = 2;
		int sum = a + b;
		int sub = a - b;
		int mul = a * b;
		float div = a / b;
		int mod = a % b;
		a++;
		b--;

		System.out.println("a + b=" + sum);
		System.out.println("a - b=" + sub);
		System.out.println("a * b=" + mul);
		System.out.println("a / b=" + div);
		System.out.println("a % b=" + mod);
		System.out.println("a의 단항증가연산 = " + a);
		System.out.println("b의 단항감소연산 = " + b);
	}
}

실행결과

a + b=7
a - b=3
a * b=10
a / b=2.0
a % b=1
a의 단항증가연산 = 6
b의 단항감소연산 = 1

2. 논리 연산자와 비트 연산자 사용 예제

class Bitwise {
	public static void main(String args[]) {
		int a = 2;
		int b = 5;
		int c = a | b;
		int d = a & b;
		int e = a ^ b;
		int i;
		int j;
		i = a << 2;
		j = b >> 2;

		System.out.println("               a = " + a);
		System.out.println("               b = " + b);
		System.out.println("           a | b = " + c);
		System.out.println("           a & b = " + d);
		System.out.println("           a ^ b = " + e);
		System.out.println("          a << 2 = " + i);
		System.out.println("          b >> 2 = " + j);
	}
}

실행결과

               a = 2
               b = 5
           a | b = 7
           a & b = 0
           a ^ b = 7
          a << 2 = 8
          b >> 2 = 1

3. 배정 연산자 사용 예제

class BitEquals {
	public static void main(String args[]) {
		int a = 10;
		int b1 = 5;
		int b2 = 5;
		int b3 = 5;

		System.out.println("a = " + a);

		a += 4;
		System.out.println("a += 4의 결과 : " + a);

		a -= 4;
		System.out.println("a -= 4의 결과 : " + a);

		a *= 4;
		System.out.println("a *= 4의 결과 : " + a);

		a /= 4;
		System.out.println("a /= 4의 결과 : " + a);

		a %= 4;
		System.out.println("a %= 4의 결과 : " + a);

		a |= 4;
		System.out.println("a |= 4의 결과 : " + a);

		System.out.println("b1 = " + b1);
		System.out.println("b2 = " + b2);
		System.out.println("b3 = " + b3);

		b1 >>= 1;
		b2 <<= 1;
		b3 >>>= 1;
		System.out.println("b1 >>= 1의 결과 : " + b1);
		System.out.println("b2 <<= 1의 결과 : " + b2);
		System.out.println("b3 >>= 1의 결과 : " + b3);
	}
}

실행결과

a = 10
a += 4의 결과 : 14
a -= 4의 결과 : 10
a *= 4의 결과 : 40
a /= 4의 결과 : 10
a %= 4의 결과 : 2
a |= 4의 결과 : 6
b1 = 5
b2 = 5
b3 = 5
b1 >>= 1의 결과 : 2
b2 <<= 1의 결과 : 10
b3 >>= 1의 결과 : 2

4. 삼항 연산자 사용 예제

class TernaryDemo {
	public static void main(String args[]) {
		int i = 10;
		System.out.print("정수형 변수 i의 값은 " + i + "이며");
		String str =| (i % 2 == 0) ? "짝수" : "홀수";
		System.out.print(str);
		System.out.println("입니다");
	}
}

실행결과

정수형 변수 i의 값은 10이며짝수입니다

Share This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • Criss Cross Crossover Crunch Flutter Kicks Heel Touch Knees Up Crunch Legs Lower Plank Side Plank Superman Arms Up Crunch Aquaman Catch Dish Oblique Crunch
  • Businesses can survive by constantly generating revenue by selling goods or services. Companies that do not have sales or profit can not survive. So it
  • Amazon CEO Jeff Bezos sends letters to shareholders every year. This letter contains Amazon’s current achievements and future vision. He also enclose that the first
  • A middle-aged man came to Pastor Norman Vincent Peale one day to ask him for counseling. The dejected man told Pastor Norman Vincent Peale. “Pastor,
  • I have been working as an IT freelancer for over 10 years and have had a chance to work with various clients. Working freelance, I
  • CNBC analyzed the minutes of the Berkshire Hathaway shareholders’ meeting to find out why Warren Buffett could be an excellent investor. It is already known,
  • Good timing is like a good choice. So what is ‘good choice’? A good choice is to decide what you need to do first. That
  • We learned that in childhood we should brush our teeth immediately after eating rice. However, this is a common misconception in the days when common
  • In most cases, it is important for novice marketers to work efficiently by reducing the waste of work and increasing focus. He will not used
  • source: vornado.com The circulator is the abbreviated name for ‘air circulator’. The air circulator actually has a different purpose than the fan. Unlike a fan