迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中从一个函数中返回空值

作者:迹忆客 最近更新:2023/09/17 浏览次数:

本文介绍如何在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便