Home > Java > Java Tutorial > body text

How to use question mark in java

下次还敢
Release: 2024-04-26 22:36:16
Original
965 people have browsed it

The question mark in Java can be used for the following purposes: Ternary operator: used for conditional judgment, returning different values ​​​​according to conditions. Null value judgment and assignment: Used to judge whether a variable is empty, and return different values ​​or assign values ​​according to conditions. Method's varargs arguments: Convert a variable number of arguments to an array. Destructuring in pattern matching: Deconstructing an object or array into multiple variables.

How to use question mark in java

Uses of question mark in Java

The question mark in Java has many uses, including:

1. Conditional operator (ternary operator)

The ternary operator is a concise conditional expression with the following syntax:

(条件) ? 值1 : 值2;
Copy after login

If the condition is true , then return value 1; otherwise return value 2.

Example:

int age = 18;
String result = (age >= 18) ? "成年" : "未成年";
Copy after login

2. Null value judgment and assignment

Null value judgment:

Question marks can be used to determine whether a variable is null. The syntax is as follows:

变量 != null ? 变量 : 默认值;
Copy after login

If the variable is not null, return the variable value; otherwise, return the default value.

Example:

String name = null;
String displayName = name != null ? name : "未知";
Copy after login

Assignment:

Question marks can also be used to assign values ​​to variables based on conditions. The syntax is as follows:

变量 = 条件 ? 值1 : 值2;
Copy after login

If the condition is true, assign the value 1 to the variable; otherwise, assign the value 2 to the variable.

Example:

boolean isAdult = true;
int ageLimit = isAdult ? 18 : 16;
Copy after login

3. The varargs parameter of the method

The varargs parameter of the method uses three dots (.. .) represents a variable number of parameters, the syntax is as follows:

public static void myMethod(String... args) { ... }
Copy after login

Question mark can convert varargs parameters into an array, the syntax is as follows:

String[] argsArray = myMethod(?);
Copy after login

4. Destructuring in pattern matching

Java 17 introduced pattern matching, where question marks are used to destructure objects or arrays. The syntax is as follows:

var (a, b) = new int[] { 1, 2 };
Copy after login

This code deconstructs new int[] { 1, 2 } into two integers type variables a and b.

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

source:php.cn
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
Popular Tutorials
More>
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!