Home> Java> javaTutorial> body text

How to use question mark and colon in Java? :expression

醉折花枝作酒筹
Release: 2021-04-28 09:17:35
forward
5855 people have browsed it

This article will introduce to you the usage of question mark and colon "?:" expressions in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use question mark and colon in Java? :expression

Let’s start with a piece of Java code:

return data != null ? data.getName() : null;
Copy after login

Okay! =The following code is divided into three parts: A, B, and C

A is a Boolean expression (true/false), B and C are execution statements.

The most intuitive: A? B: C, which means that if A is true, execute B, otherwise execute C

The above code means that if data is not empty, return data.getName(), if data is empty, return null.

Extension

After reading the above explanation, let’s extend it again

2 ==0 ? 1 1: 2-2;

Such as 2 Equal to 0, execute 1 1. If 2 is not equal to 0, execute 2-2.

Recommendation: "java Video Tutorial"

The above is the detailed content of How to use question mark and colon in Java? :expression. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!