Home  >  Article  >  Java  >  How to use question mark in java

How to use question mark in java

下次还敢
下次还敢Original
2024-04-26 22:36:161084browse

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:

<code>(条件) ? 值1 : 值2;</code>

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

Example:

<code class="java">int age = 18;
String result = (age >= 18) ? "成年" : "未成年";</code>

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:

<code class="java">变量 != null ? 变量 : 默认值;</code>

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

Example:

<code class="java">String name = null;
String displayName = name != null ? name : "未知";</code>

Assignment:

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

<code class="java">变量 = 条件 ? 值1 : 值2;</code>

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

Example:

<code class="java">boolean isAdult = true;
int ageLimit = isAdult ? 18 : 16;</code>

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:

<code class="java">public static void myMethod(String... args) { ... }</code>

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

<code class="java">String[] argsArray = myMethod(?);</code>

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:

<code class="java">var (a, b) = new int[] { 1, 2 };</code>

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!

Statement:
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