Home > Java > javaTutorial > How to use java conditional operators

How to use java conditional operators

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-05-11 10:16:05
forward
1845 people have browsed it

Concept

1. The conditional operator is also called the ternary operator. This operator has three operands and needs to determine the value of a Boolean expression.

2. This operator mainly determines which value should be given to the variable.

Grammar form

布尔表达式 ? 表达式1 :表达式2
Copy after login

Operation process

If the value of the Boolean expression is true, the expression is returned The value of expression 1, otherwise the value of expression 2 is returned.

public static void main(String[] args) {
    int a, b;
    a = 10;
    // 如果 a 等于 1 成立,则设置 b 为 20,否则为 30
    b = (a == 1) ? 20 : 30;
    System.out.println("Value of b is : " + b);
 
    // 如果 a 等于 10 成立,则设置 b 为 20,否则为 30
    b = (a == 10) ? 20 : 30;
    System.out.println("Value of b is : " + b);
}
Copy after login

The above is the detailed content of How to use java conditional operators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template