본문 바로가기

JAVA

[32일차] 자바 연산자

package test;

public class Test83 {

	public static void main(String[] args) {
		try {
			System.out.println("시작");
			int z= 5/0;
			System.out.println("결과:"+z);
			
		}catch(ArithmeticException e) {
			System.out.println("0으로 나누면 안됨!!");
			System.out.println(e); //예외 구문을 알려줌 
			
		}finally {
			System.out.println("프로그램 종료");
		}
	}
}

 

package test;

public class Test84 {

	public static void main(String[] args) {
		try {
			System.out.println("시작");
			double z= 5/1.5;
			System.out.println("결과:"+Double.isNaN(z)); //숫자가 아니면 참 
			System.out.println("결과:"+Double.isFinite(z)); // 유한 값이면 참
			System.out.println("결과:"+Double.isInfinite(z));// 값이 무한대이면 참
			
		}catch(ArithmeticException e) {
			System.out.println("0으로 나누면 안됨!!");
			System.out.println(e); //예외 구문을 알려줌 
			
		}finally {
			System.out.println("프로그램 종료");
		}
	}
}

 

프로그램 코드에서 / 와 % 연산의 결과가 Infinite(무한대), NaN(Not a Number)인지 확인하려면 

Double.isInfinite() , Double.isNaN() 메소드를 이용하면 된다 

이 메소드들은 double타입의 값을 매개값으로 받아서 이 값이  Infinite, NaN 이면 true를 리턴하고 아니면 false를 리턴한다

double z= 5/0.0; 와 double z= 5/0; 했을때 출력값이 다르게 나옴 

package test;

import java.util.Scanner;

public class Test84 {

	public static void main(String[] args) {
		try {
			
			Scanner z= new Scanner(System.in);//키보드에서 값을 받아서 출력, 실수가 아닌값이 들어오면 오류발생
			double z1= z.nextDouble();
			
			if(Double.isInfinite(z1)||Double.isNaN(z1)) {
				System.out.println("연산불가");
			}else {
				System.out.println(z1+2);
			}
			
			int k1=0;
			double k2= 0.0;
			
			if(k1==k2 ) {
				System.out.println("같다");
			}
			
			
		}catch(ArithmeticException e) {
			System.out.println("0으로 나누면 안됨!!");
			System.out.println(e); //예외 구문을 알려줌 
			
		}finally {
			System.out.println("프로그램 종료");
		}
	}
}

 

화면에 7.5 입력했을때

 

화면에 '우리' 입력했을 때

 


문자열 연결 연산자 (+)

package test;

public class Test90 {

	public static void main(String[] args) {
	
		String str1= "신용권";
		String str2= "신용권";
		
		if(str1==str2) {
			System.out.println("같다");
		}
	}
}

 

같다 라고 출력

package test;

public class Test90 {

	public static void main(String[] args) {
	
		String str1= "신용권";
		String str2= "신용권";
		String str3= new String ("신용권"); //신용권 출력 
		System.out.println(str3);
		
		if(str1==str3) {
			System.out.println("같다"); //str1==str2 같다 출력
			
		}else {
			System.out.println("같지않다");//str1==str3 같지않다 출력 
		}
	}
}
//값이 같은걸 물어보는게 아니라 같은 메모리인지 비교하는 것

new 는 새로운 메모리공간을 만들어서 저장함 

따라서 각각 다른 영역인 것 

String str3= new String ("신용권"); 
String str4= new String ("신용권");

if(str4==str3) {
System.out.println("같다"); 

}else {
System.out.println("같지않다");
}

--> 같지않다 출력 

 

따라서 결과값을 비교할때 문자열은 equals 로 한다 

package test;

public class Test90 {

	public static void main(String[] args) {
	
		String str1= "신용권";
		String str2= "신용권";
		String str3= new String ("신용권");
		String str4= new String ("신용권");
		System.out.println(str3);
		
		if(str4.equals(str3)) {
			System.out.println("같다"); 
			
		}else {
			System.out.println("같지않다");
		}
	}
}

같다 라고 출력 

 

== 주소공간 비교
/equals() 값을 비교하는것 

* 변수는 값이 같으면 같은 공간을 사용 해서 str1, str2 는 같다고 출력되는 것 


논리 연산자 (&&, ||, &, |, ^, ! )

package test;

public class Test90 {

	public static void main(String[] args) {
		int a=20;
		int b= 10;
		//  참         거짓
		if(a>=10 && b>=20) {	
			System.out.println("참");
		}else {
			System.out.println("거짓"); //거짓출력
		}
	    //  참         거짓
		if(a>=10 || b>=20) {	
			System.out.println("참"); //참 출력 
		}else {
			System.out.println("거짓");
		}
	}
}

거짓

출력


삼항연산자 

package test;

public class Test100 {

	public static void main(String[] args) {
		int score=95;
		String grade= (score >=95)?"합격":"불합격";
		System.out.println(grade);
	}
}

합격 출력 

 

package test;

public class Test100 {

	public static void main(String[] args) {
		int score=95;
		String grade= (score >=99)?"합격":"불합격";
		System.out.println(grade);
		
		int score2 =80;
		String grade2= (score2>=90)? "수": ((score2 >=80) ? "우":"미달");
		System.out.println(grade2);
	}
}


---------------
불합격
우

random 함수 

 

 

'JAVA' 카테고리의 다른 글

[34일차] 자바 패키지 하나 더 만들기  (0) 2022.07.07
[33일차]  (0) 2022.07.06
[32일차] 자바 메소드 만들기  (0) 2022.07.05
[31일차] java 기본  (0) 2022.07.04
[ 30일차 ] JDK,아파치톰캣,이클립스 설치 / 환경설정  (0) 2022.07.01