简体   繁体   English

当方法定义为 int 数据类型时,如何在 Java 中返回 null?

[英]How can I return null in Java when the method is defined as int data type?

I've been doing a code where its mandatory to use the data type cannot be void and I don't need to return anything.我一直在做一个代码,它强制使用数据类型不能是无效的,我不需要返回任何东西。

In Java, null is only a valid value for reference types.在 Java 中, null只是引用类型的有效值。 It cannot represent a primitive type such as int .它不能表示原始类型,例如int Here are some alternatives to consider:以下是一些可供考虑的替代方案:

  1. If you are using Java 8 or later, and are able to change the return type of the method you could use the OptionalInt type to represent an int value that may or may not be present.如果您使用的是 Java 8 或更高版本,并且能够更改方法的返回类型,则可以使用OptionalInt类型来表示可能存在或不存在的int值。 In that case, you would return OptionalInt.empty() in the case that there is no int value to return, and OptionalInt.of(x) to return an int x .在这种情况下,如果没有要返回的int值,您将返回OptionalInt.empty() ,而OptionalInt.of(x)返回int x Note that the caller will need to unwrap the int value (if it is present) using one of the other methods available on that class.请注意,调用者需要使用 class 上可用的其他方法之一来解开int值(如果存在)。 This approach is often preferred for new code, as it makes the intention and usage very clear.这种方法通常是新代码的首选,因为它使意图和用法非常清楚。
  2. If you are using an older Java version, another possibility is to change the return type to Integer .如果您使用的是较旧的 Java 版本,另一种可能性是将返回类型更改为Integer This is a wrapper class for int values that does allow for null to be returned.这是一个包装器null用于允许返回 null 的int值。 In addition, Java's auto-unboxing rules allow it to be used in contexts where an int value is expected, when the value is not null .此外,当值不是null时,Java 的自动拆箱规则允许它在需要int值的上下文中使用。 However, if a null value is unboxed to an int value, it will result in a NullPointerException , so it is important to check for null before performing operations that would result in unboxing.但是,如果将null值拆箱为int值,则会导致NullPointerException ,因此在执行可能导致拆箱的操作之前检查null非常重要。
  3. If you need to use the int return type, it is common to use a sentinal value to represent an abnormal return.如果需要使用int返回类型,通常使用一个哨兵值来表示异常返回。 For example, if the normal return values for the method are all non-negative integers, you could use a value such as -1 to represent an absent return value.例如,如果该方法的正常返回值都是非负整数,则可以使用诸如-1之类的值来表示不存在的返回值。 This is commonly used in older JDK methods such as String.indexOf() .这通常用于较旧的 JDK 方法,例如String.indexOf()
  4. In some cases, it makes sense to throw an Exception when no valid value can be returned.在某些情况下,当无法返回有效值时抛出Exception是有意义的。 It's only a good idea to use this approach for truly exceptional circumstances, as the runtime cost for throwing exceptions is much higher than normal method returns, and the flow of control can make the code harder to understand.仅在真正异常的情况下使用这种方法是一个好主意,因为抛出异常的运行时成本远高于正常的方法返回,并且控制流会使代码更难理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单个 Java 方法中返回 int 和 string 属性? - How can I return int and string attributes in a single Java method? 如何在 Java 的 get 和 return 方法中使用 input(int)? - How can i use input(int) in get and return method in Java? TObjectIntMap(Trove 3)'int get(java.lang.Object key)'方法如何返回null? 是文档错误吗? - TObjectIntMap (Trove 3) How can 'int get(java.lang.Object key)' method return null? Is it a documentation mistake? 如何将null返回给int方法 - how to return null to int method 如何在不使用递归的情况下使用 int 返回类型方法返回 FIbonacci 序列? - How can I return the FIbonacci sequence using an int return type method without using recursion? 如果Java方法的返回类型是int,那么该方法是否可以返回byte类型的值? - If a Java method's return type is int, can the method return a value of type byte? 如何在不破坏当前代码的情况下修复错误“此方法必须返回 int 类型的结果”? - How can I fix the error "This method must return a result of type int" without breaking the current code? “此方法必须返回int类型的结果” Java吗? - 'This method must return a result of type int' Java? 如何为返回类型为int的方法不返回任何内容 - How to not return anything for a method with return type int 我在Java中有“”时如何返回null? - How to return null when I have “ ” in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM