在 Java 中从一个函数中返回空值
本文介绍如何在 Java 中的函数中不返回任何内容。
每个在其签名中具有返回类型的函数都必须返回相同类型的值,否则编译将产生错误,并且代码将不会执行。这意味着返回值是返回给调用者函数的值,如果我们希望什么都不返回,那么我们将不得不返回一些默认值。
例如,我们可以为字符串类型返回 null
,为整数值返回 0
来处理这种情况。让我们通过一些例子来理解。
在 Java 中的整数函数中不返回任何内容
在此示例中,我们有一个返回整数的静态方法,但在特殊情况下我们不希望返回任何内容,那么我们只返回 0
。尽管整数中没有要返回的内容,但我们可以这样做来实现我们的目标。
请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args){
int result = getMax(12, 12);
System.out.println(result);
}
public static int getMax(int a, int b) {
if(a>b) {
return a;
}
else if (b>a) {
return b;
}
return 0; // similar to nothing in int
}
}
输出:
0
在 Java 中的字符串函数中不返回任何内容
在这个例子中,我们有一个返回字符串的静态方法,但在特殊情况下我们不希望返回任何内容,那么我们只返回 null
。尽管 String 中没有什么可以返回,但我们可以实现我们的目标。
请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args){
String result = getMax("rohan", "sohan");
System.out.println("result "+result);
result = getMax("sohan", "sohan");
System.out.println("result "+result);
}
public static String getMax(String a, String b) {
if(a.compareTo(b)>0) {
return a;
}
else if (a.compareTo(b)<0) {
return b;
}
return null; // similar to nothing in String
}
}
输出:
result sohan
result null
在 Java 中的任何对象函数中都不返回任何内容
在此示例中,我们有一个返回整数对象的静态方法,但在特殊情况下我们不希望返回任何内容,那么我们仅返回 null
,因为 null
是 Java 中对象的默认值。尽管返回的对象中没有什么像什么都没有,但我们可以实现我们的目标。
我们也可以将它用于任何其他对象。请参见下面的示例。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SimpleTesting{
public static void main(String[] args){
List<Integer> list = new ArrayList<>();
list.add(23);
list.add(32);
list.add(33);
System.out.println(list);
Integer result = getMax(list);
System.out.println("result "+result);
}
public static Integer getMax(List<Integer> list) {
if(Collections.max(list)>100) {
return 100;
}
else if (Collections.max(list)<50) {
return Collections.max(list);
}
return null; // similar to nothing in Integer object
}
}
输出:
[23, 32, 33]
result 33
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。