>  기사  >  Java  >  Java의 부울 연산자

Java의 부울 연산자

WBOY
WBOY원래의
2024-08-30 15:20:36502검색

기술의 출현과 함께 컴퓨터가 발전했고, 그에 따라 프로그래밍 언어에 대한 요구도 생겼습니다. 많은 프로그래밍 언어에는 고급 언어와 저급 언어가 모두 포함되어 있습니다. 고급 언어는 저급 언어에 비해 이해하기 쉽기 때문에 사용하기가 더 쉽습니다. Java는 프로그래밍 목적의 지원 언어로 널리 사용되는 고급 언어 중 하나입니다. 기본 개념을 이해하기 위해서는 공부하고 연습해야 할 개념이 많이 있습니다. 이 주제에서는 Java의 부울 연산자에 대해 설명합니다.

광고 이 카테고리에서 인기 있는 강좌 JAVA MASTERY - 전문 분야 | 78 코스 시리즈 | 15가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

부울 연산자란 무엇인가요?

부울 연산자는 단순히 표현식을 비교하는 데 사용할 수 있는 다양한 연산자 집합입니다. 부울 연산자에는 일반적으로 false 또는 true라는 두 가지 값이 있습니다. 부울 연산자는 왼쪽과 오른쪽의 표현식을 비교합니다. 이에 비해 단순히 부울 값을 반환합니다.

Java의 부울 연산자 유형

Java에는 다양한 유형의 부울 연산자가 있습니다. 다음은 가장 널리 사용되는 Java의 다양한 부울 연산자 유형입니다.

Java의 부울 연산자

1. 논리 AND 연산자

&& 연산자를 사용하여 논리식을 비교하는 논리 할당입니다. 일반적으로 여러 논리 중 하나라도 실패하면 false를 제공하고 모든 표현식이 true를 생성하면 true를 제공합니다.

AND 연산자의 예
  • 두 피연산자가 모두 참이면 연산 결과도 참입니다

코드:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = true;
System.out.println (a && b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • 두 피연산자가 모두 거짓이면 연산 결과도 거짓입니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
System.out.println (a && b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • 피연산자 중 하나가 true이고 다른 하나가 false인 경우 연산 결과는 false입니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println (a && b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • 한 피연산자가 false이고 다른 피연산자가 true이면 연산 결과는 false입니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println (a && b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

2. 논리 OR 연산자

이것은 ||를 사용하는 논리적 할당입니다. 논리식을 비교하는 연산자입니다. 일반적으로 표현식 중 하나라도 true가 되면 true를 반환하고 모든 표현식이 실패하면 false를 반환합니다.

OR 연산자 예시
  • 두 피연산자가 모두 참이면 연산 결과도 참이 됩니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = true;
System.out.println (a || b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • 두 피연산자가 모두 거짓이면 연산 결과도 거짓입니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
System.out.println (a || b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • 피연산자 중 하나가 true이고 다른 하나가 false이면 연산 결과는 true입니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println (a || b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • 한 피연산자가 false이고 다른 피연산자가 true이면 연산 결과는 true입니다.

코드:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println (a || b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

3. 연산자와 같음

이 연산자는 연산자 양쪽의 피연산자나 식이 같은지 여부를 확인하는 데 사용됩니다.

동등 연산자의 예
  • 두 피연산자가 동일하지 않으면 연산 결과는 false입니다.

코드:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abcd";
System.out.println (a == b); // shows the logical operation using operator
}
}

이제 위 코드를 실행해 보세요

출력:

Java의 부울 연산자

  • If both operands are the same, the operation result is true.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println (a == b); // shows the logical operation using operator
}
}

Now, execute the above code

Output:

Java의 부울 연산자

4. Not Equal to Operator

This operator is used to check if operand or expression on both sides of the operator are equal or not. It produces true if operands on both sides are not the same; else gives false.

Examples of not equal to operator
  • If both operands are not the same, the operation result is true.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abcd";
System.out.println (a != b); // shows the logical operation using operator
}
}

Now, execute the above code

Output:

Java의 부울 연산자

  • If both operands are the same, the operation result is false.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println (a != b); // shows the logical operation using operator
}
}

Now, execute the above code

Output:

Java의 부울 연산자

5. Ternary Operator

This operator is used to check if else conditions. It is generally shorthand for the if-else statement. If the expression is true, then if the part is executed otherwise, else block is executed. It uses two operands which are ?:

Example of Ternary Operator
public class Main
{
public static void main (String[]args){
int a = 2;
int b = 5;
int minOfNum = (a < b) ? a : b;
System.out.println (minOfNum);
}
}

Output:

Java의 부울 연산자

In expression, (a < b) ? a : b it evaluates the value. Based on the evaluation, it executes if or else block

Conclusion

Java is a programming language where there are lots of concepts that one needs to study. Boolean operators are one of those. These boolean operators basically execute the code to check whether the expression value is true or not. Based on the expression evaluation, it returns the value. A boolean operator is widely used in any programming language to various logical programming expressions.

위 내용은 Java의 부울 연산자의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.