연산자

서현서현·2022년 2월 9일
0

JAVA

목록 보기
5/27
post-thumbnail

🍇 연산자

: 연산에 사용되는 표시나 기호를 말한다

  • 연산자의 종류

🍇 단항 연산자

  • 부호 연산자 : +와 -를 말한다. 산술연산자이자 부호연산자인것. 부호연산자로 쓰일때 피연산자는 하나만 있으면 된다.
    (ex) int i = -100; <- 피연산자 하나

🚨 부호연산자로 사용될 때 주의할점

byte b = 100;
byte result = -b; > 이때 컴파일에러가 발생한다. 부호연산의 결과는 int이므로 int result여야함!
  • 증감 연산자 : ++,--
    변수 앞에 연산자가 붙으면 해당변수를 먼저 연산한 후 식을 실행하고, 뒤에붙으면 식을 실행한 뒤 연산을 수행한다.

  • 논리 부정 연산자 : boolean
    부정연산을 위해 사용, True와 False

증감연산자 예제

package chapter03;

 public class IncreaseDecreaseOperator {
     public static void main(String[] args) {
         int x=10;
         int y=10;
         int z;
		
		System.out.println("---------------------");
		x++;
		++x;
		System.out.println("x="+x);
		
		System.out.println("---------------------");
		y++;
		++y;
		System.out.println("y="+y);
		
		System.out.println("---------------------");
		z= x++; //x는 12니까 z 그대로 들어가고 연산 끝났으니 ++수행, x는 13
		System.out.println("z="+z); 
		System.out.println("x="+x);
		
		System.out.println("---------------------");
		z=++x;  //x는 13이였는데 ++수행해서 14돼서 z에 들어감
		System.out.println("z="+z);
		System.out.println("x="+x);
		
	}
	}

🍇 이항연산자

  • 산술 연산자 : +, -, *, /, %
  • 문자열 결합 연산자 : +
  • 비교 연산자 : <, <=, >, >=, ==, !=
  • 논리 연산자 : &&, ||, &, |, ^, !
  • 대입연산자 : =, +=, -=, *=, /=, %=

비교연산자 예제

package chapter03;

public class CompareOperatorExample1 {
 public static void main(String[] args) {
	 int num1=10;
	 int num2=10;
	 boolean result1 = (num1 == num2);
	 boolean result2 = (num1 != num2);
	 boolean result3 = (num1 <= num2);
	 System.out.println("result1= "+result1);
	 System.out.println("result1= "+result2);
	 System.out.println("result1= "+result3);
	 
	 char char1 = 'A';
	 char char2 = 'B';
	 boolean result4 = (char1 <char2);
	 System.out.println("result4= "+result4);
	 
	 float v4 = 0.124f; //0.123999
	 double v5 = 0.125;  //0.12399999999999
	 System.out.println((double)v4==v5);  //v4가 더블이되면 0.12399999999.더커지니 F
	 System.out.println(v4==(float)v5);  // 0.123999가됨. 즉 작은수로 캐스팅하면 T
	 
 }
}

논리연산자 예제

package chapter03;

import java.util.Scanner;

public class LogicalOperatorExample {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("문자 하나를 입력하세요:");
		int charCode = scanner.next().charAt(0); //A
		System.out.println(charCode >= 65 && charCode <=90); //대문자면 T 소문자면 F
		
		System.out.println("숫자를 입력하세요:");
		int value = scanner.nextInt();
		System.out.println(value%2==0||value%3==0); //||두개면 앞에만 봐도 판단 가능할때 뒤는 안봄
		System.out.println(value%2==0||value%3==0); // |이거는 무조건 둘다봄
	}

}

대입연산자 예제

public class AssignmentOperatorExample {
	public static void main(String[] args) {
		
		int result = 0;
		result+= 10;
		System.out.println("result = "+result);
		
		result-= 5;
		System.out.println("result = "+result);
		
		result*= 3;
		System.out.println("result = "+result);
		
		result/= 5;
		System.out.println("result = "+result);
		
		result%= 3;
		System.out.println("result = "+result);
	
	
	}
}

🍇 삼항연산자

  • (조건식) ? (값 또는 연산식) : (값 또는 연산식)

0개의 댓글