Java 연산자

Ryu·2021년 2월 25일
0

java

목록 보기
6/7
post-thumbnail

연산자

연산자는 산술 관계 논리순으로 우선순위가 결정된다.

문자열 뒤에 숫자가 오면 숫자도 문자열로 바뀌어 연산된다.

public class Ex1 {
public static void main(String[] args) {
	
	int number = 9;
	System.out.println(number/4); 
	
	double number2 = 9;
	System.out.println(number2/4);
	
	
	System.out.println(number2%4);
	
	System.out.println("JDK" + 6.0);
	String st1 = "JDK" + 6.0;
	String st2  =st1 + "특징";
	System.out.println(st2); // JDK6.0특징
	String st3 = 3 + 3.0 + "JDK";
	System.out.println(st3); // 6.0JDK
	
}	
}

++가 앞에 나오면 원래 지정한 변수에서 1씩 증가한것이고 뒤에 ++나오면 원래 변수가 먼저나오고 그뒤부터 1씩 증가한다.

public class Ex2 {

	public static void main(String[] args) {
		
		// ++ 1더하기 -- 1빼기
		
		int num1 = 1;
		System.out.println(++num1); // 2
		
		int num2 = 1;
		System.out.println(num2++); // 1
		System.out.println(num2++); // 2
	}
}
	int a = 0;
	int b = -1;
	int c = ++a - --b + b + a--;
	System.out.println(c); // 1 - (-2) + (-2) + (1)

~는 비트반전 연산자

int num = 7;
		System.out.println(~num); // -8
		
		byte v1= 27;
		int v2=~v1;
		System.out.println(v2); // -28

비교연산자

public class Ex4 {

	public static void main(String[] args) {
		
//		int num1 = 10;
//		int num2 = 10;
//		System.out.println(num1 != num2);

		char char1='A';
		char char2='B';
		
		System.out.println(char1<char2);
		// 피연산자가 char타입이면 유니코드 값으로 비교연산을 수행한다.
		
		System.out.println('A'==65);
		System.out.println(3==3.0);
		System.out.println(0.1==0.1f);
		// 모든 부동 소수점 타입은 0.1을 정확히 표현 할 수가 없어서 0.1f는 0.1의 근사값으로 표현되어
		// 0.10000000149011612같은 값이 되기 떄문에 0.1보다 큰값이 되어버린다.
		// 해결책은 피연산자를 모두 float 타입으로 강제 타입 변환한 후에 비교연산을 하던가, 정수로 변환해서 비교하면 된다.
		
		int v2=1;
		double v3=1.0;
		System.out.println(v2==v3);
		
		double v4=0.1;
		float v5=0.1f;
		System.out.println(v4==v5);
		
		System.out.println((float)v4==v5);
		
		System.out.println(v4*10);
		System.out.println(v5*10);
		System.out.println((int)(v4*10)==(int)(v5*10));
	
//		for(int i=0;i<100;i++) {
//		System.out.println((int)((Math.random()*6)+1));
		}
	}

문자열 비교

public class Ex6 {

	public static void main(String[] args) {
		
		String str1="abc";
		String str2=new String("abc");
		System.out.println(str1);
		System.out.println(str2);
		
		System.out.printf("%b\n", "abc" =="abc");
		System.out.printf("%b\n", str1 =="abc");
		System.out.printf("%b\n", str2 =="abc");
		System.out.printf("%b\n", str2.equals("abc"));
		System.out.printf("%b\n", str2.equalsIgnoreCase("ABC"));
		// 문자열 비교는 항상 equals()로 한다. equals()는 객체가 달라도 내용이 같으면 true를 반환한다.
		
		
	}
}

논리 연산자

public class Ex7 {

	public static void main(String[] args) {
		
		//논리 연산자 (논리연산자(&&, ||, !), 비트논리(&, |, ~)연산자가 있다.
		int charCode = 'a';
		if((charCode>=65) && (charCode<=90)){
				
 		System.out.println("대문자");
		} else {
			System.out.println("대문자가 아님");
		}
 		int value=6;
 		if((value%2==0)|| ((value%3==0))) {
 			System.out.println("2 또는 3의 배수");
 		}

		
		
	}
}

비교연산자 계산 연습

public class Ex8 {

	public static void main(String[] args) {
		
		System.out.println(45 & 25); // 9
		System.out.println(45 | 25); // 61
		System.out.println(45 ^ 25); // 52
		System.out.println(~45); // -46
		
		// 음의 정수 -10, 10을 얻으려면 공식화하면 음의정수 n이 있을떄 양의정수를 얻는 방법 ~(n-1)
		
		int result = 1 << 3;
		System.out.println(result);
		
		System.out.println(~8>>3);
		int score=75;
		System.out.println(score>90 ?'A' : (score>80? 'B' : 'C'));
		
		
		
	}
		
}
profile
쓴다.노트.하는동안.공부

0개의 댓글