>  기사  >  Java  >  자바 부울

자바 부울

王林
王林원래의
2024-08-30 15:11:58217검색

오늘날까지 우리는 종종 활동, 상황, 비전, 결과, 사건 등에 대해 결정을 내립니다. 우리 결정의 가치는 예 또는 아니오라는 두 가지 중 하나입니다. 참 또는 거짓; 켜거나 끄기; go 또는 no-go 등. 프로그래밍은 어떤 예외에도 해당되지 않습니다. 프로그래밍에서는 핵심 논리와 사용 사례를 기반으로 결정을 내리고 이러한 결정을 기반으로 해야 합니다. 그에 따라 코드를 작성해야 합니다. 프로그래밍 언어로서 Java도 예외는 아니며 의사 결정을 위해 코드에서 사용할 수 있도록 "Boolean"이라는 특수 데이터 유형을 제공할 수 있습니다. Java 부울 변수 또는 부울 표현식은 true 또는 false의 두 값 중 하나를 취할 수 있습니다.

Java 프로그래밍 관점에서 부울에 대해 논의해 보겠습니다.

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

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

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

Java 불리언 값의 종류

다음은 다양한 유형의 Java 부울 값입니다.

자바 부울

1. 변수 이름을 사용한 키워드 부울

Java의 부울 유형 변수 값과 관련하여 두 가지 옵션만 있습니다. 부울 유형의 값은 true 또는 false입니다. 다른 옵션은 없습니다. Java 숙제나 작업에 대한 도움을 서두르지 마세요. Java 부울을 이해하려면 계속 읽어보세요. 변수 이름과 함께 키워드 부울을 사용하고 값(true 또는 false)을 할당해야 합니다.

구문:

Boolean <variable_name> = <value>, where value is either true or false

예:

boolean bool = true, 여기서 bool은 변수 이름이고 true 값과 연결됩니다.

boolean bool2 = false, 여기서 bool은 변수 이름이고 false 값과 연결됩니다.

코드 예 1:

public class BooleanInJava {
public static void main(String[] args){
boolean bool = true ;
boolean bool2 = false;
System.out.println(bool);
System.out.println(bool2);
}
}

출력:

자바 부울

2. 불리언형 변수

부울형 변수에 true, false 이외의 값을 제공하면 어떻게 되나요?

예:

부울 부울 = 1 ;

부울 bool2 = 0;

이렇게 하면 오류가 발생합니다.

코드 예 2:

public class BooleanInJava {
public static void main(String[] args) {
boolean bool = 1 ;
boolean bool2 = 0;
System.out.println(bool);
System.out.println(bool2);
}
}

출력:

자바 부울

3. 부울의 특징

이제 Boolean의 이 기능을 어떻게 효과적으로 사용할 수 있을까요?

프로그램에서 결정을 내리는 데 이를 사용할 수 있습니다. 조건 연산자를 사용하여 부울 값을 얻거나 인쇄함으로써 프로그램의 일부 결정 요인을 테스트하는 데 사용할 수 있다는 의미입니다. 이는 부울 표현식의 상태를 테스트하는 것입니다. 프로그램은 이 표현을 평가하고 그에 따라 결정을 내릴 것입니다.

몇 가지 예를 들어보겠습니다.

코드 예 3:

public class BooleanInJava {
public static void main(String[] args) {
int num1 = 10;
int num2 =11;
System.out.println(num1 > num2); // returns false, because 11 is higher than 10
System.out.println(num2 > num1); // returns true, because 11 is higher than 10
System.out.println(num1 < num2); // returns true, because 10 is lesser than 11
System.out.println(num2 <num1); // returns false, because 10 is lesser than 11
}
}

출력:

자바 부울

부울 값은 어떻게 작동하나요?

이 기사에서는 Boolean의 작동 방식, 즉 프로그램이나 사용 사례에서 Boolean 기능을 사용할 수 있는 방법을 설명합니다. 부울은 결정을 내리는 데 도움이 되므로 while 루프 평가 또는 if-else 의사 결정과 같은 조건식 내부에 이 결정 논리를 넣을 수 있습니다. 먼저 부울 표현식에서 부울 값을 생성하고 최종적으로 결정을 내릴 때 해당 값을 사용하는 데 사용되는 부울 연산자를 살펴보겠습니다. 여기서는 다음과 같은 부울 논리 연산자를 사용합니다. , & , ^ , ! , || , && , == , != . 두 개의 부울 변수 num1과 num2를 사용하겠습니다.

Symbol of Boolean operators Name of the corresponding Symbol
| OR
& AND
^ XOR
! NOT
!= NOT EQUAL
&& Short circuit AND
|| Short circuit OR
== EQUAL
부울 연산자 기호 해당 기호 이름 | 또는 & 그리고 ^ XOR ! 아님 != 같지 않음 && 단락 및 || 단락 또는 == 같음

Please check the table for your understanding of how evaluation is happening in Boolean expressions. This understanding is very important to clear your concepts:

Variables/Boolean Expressions num1 num2 num1|num2 num1&num2 num1^num2 !num1 !num2
 

Values/Result of evaluations

true true true true false false false
true false true false true false true
false true true false true true false
false false false false false true

true

4. Public Class

Code Example 4: 

public class BooleanInJava {
public static void main(String[] args) {
boolean num1 = true;
boolean num2 = false;
System.out.println("num1|num2 = "+(num1|num2));
System.out.println("num1&num2 = "+(num1&num2));
System.out.println("num1^num2 = "+(num1^num2));
System.out.println("!num1 = "+(!num1));
System.out.println("!num2 = "+(!num2));
}
}

Output:

자바 부울

Let us see some more code examples.

5. Boolean Operators

Code Example 5:

Here we will compare two Boolean variables and assign values to them and then create Boolean expression for those using Boolean operators and then print them to see the final output.

public class BooleanInJava {
public static void main(String[] args) {
boolean num1 = true;
boolean num2 = false;
boolean num3=(num1==num2); // Boolean expression evaluating whether values of num1 and num2 are equal or not
System.out.println(num1);
System.out.println(num2);
System.out.println(num3); //will return false as num1 and num2 have different values
}
}

Output:

자바 부울

6. Boolean Objects.

Code Example 6:

Here we will compare two Boolean objects.

public class BooleanInJava {
public static void main(String[] args) {
boolean boolObj1=new Boolean("TRUE");
boolean boolObj2=new Boolean("FALSE");
boolean boolObj3=new Boolean("FALSE");
boolean decision=(boolObj1==boolObj2);  // evaluating values of boolObj1 and boolObj2
System.out.println("Are the value of boolean objects 1 and 2 equal? "+decision);
boolean decision2=(boolObj3==boolObj2);  // evaluating values of boolObj2 and boolObj3
System.out.println("Are the value of boolean objects 2 and 3 equal? "+decision2);
}
}

Output:

자바 부울

Conclusion – Java Boolean

All of the comparisons and conditions in Java are primarily based on Boolean expressions; hence you need to use them in an effective manner. In this topic, you have learned about many aspects of Boolean values but, you need to use them effectively based on your business/ client requirements and use cases.

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

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