迹忆客 专注技术分享

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

5 个你不知道的 Java 方法

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

Java 语言充满了可供程序员在其程序中导入和使用的现成工具。 类及其方法通常会简化事情,让我们能够高效地理解和编写代码。

涵盖大部分基本算法和复杂流程,内置类和方法是每个程序员必须知道的。

下面是 5 个 Java 方法,它们通常不常见,但可以证明在我们的整个编程过程中都有重要用途。

1. decrementExact

decrementExact() 是 java 中 Math 类的 一个基本函数,它将给定参数(一个数字)减一,并返回结果。 此函数与 incrementExact() 函数相反。

如果给定的参数是 11,那么结果将是 10

如果递减参数溢出其数据类型,则会抛出异常。 因此,使用此函数时务必小心,尤其是对于较大的数字。 通常,整数用于此函数。

语法

Math.decrementExact(number);

示例

System.out.println(Math.decrementExact(11));
// 输出: 10

2. getAsDouble

getAsDouble() 是属于 OptionalDouble 类的方法。 OptionalDouble 对象是一个可能包含双精度数的对象。 类中的方法可用于处理对象中存在的双精度值,或指定根本不包含双精度值。

getAsDouble() 是这些方法之一,如果存在,它实际上返回双精度值。 否则,将抛出 NoSuchElementException

语法

OptionalDoubleObject.getAsDouble();

示例

OptionalDouble num = OptionalDouble.of(15.0);
System.out.println(num.getAsDouble());
// 输出: 15.0

3. absExact

absExact() 类似于 Math 类中的 abs() 函数; 它返回数字的绝对值,即数字的正值而不考虑其符号。

然而,不同之处在于它只有在可以精确表示为它的数据类型(int 或 long)时才会这样做。

如果生成的返回值溢出原始数据类型,则会抛出 ArithmeticException

语法

Math.absExact(number);

示例

System.out.println(Math.absExact(-11));
// 输出: 11

4. endsWith

endsWith() 方法是一个内置的字符串方法,它根据给定的字符串是否以参数中指定的后缀(结束词/字符串)结尾来返回一个布尔值。

该方法与 startsWith() 方法相反,可能更多人比较熟悉。

语法

String.endsWith(String suffix);

示例

String phrase = "I like bananas";
System.out.println(phrase.endsWith("bananas")); // true
System.out.println(phrase.endsWith("Tandrew")); // false
/* 输出:
true
false
*/ 

5. divideUnisgned

divideUnsigned() 方法是 Integer 类中的一个方法,它允许我们将两个数字相除并返回一个无符号商值。

与常规有符号整数相比,无符号整数只能表示正数。 无符号整数和有符号整数在其范围内具有相同数量的数字(范围大小均为 65 536 个数字)。

但是,由于无符号整数不能为负,因此它们在正数范围内的最大值远高于常规有符号整数的最大值。

为了简化这一点,我们可以改为查看有符号和无符号字节的示例。 字节的范围为 256 个数字。 常规字节可以从 -128127。但是,无符号字节可以从 0 一直到 255

除此之外,该函数的工作方式与常规除法完全相同。

语法

Integer.divideUnsigned(int dividend, int divisor);

示例

int dividend = 10;
int divisor = 5;
int quotient = Integer.divideUnsigned(dividend, divisor);
System.out.println(quotient);
// 输出: 2

总结

以下是本文中讨论的函数/方法的简要总结:

  • decrementExact() — 将指定的数字减 1
  • getAsDouble()OptionalDouble 函数的一部分,返回给定可选双精度对象中的数字(如果存在)
  • absExact() — 如果数字可表示为其原始数据类型,则返回数字的绝对值
  • endsWith() — 根据给定字符串中是否存在指定的后缀返回一个布尔值
  • divideUnsigned() — 执行常规除法但返回无符号的商值

转载请发邮件至 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

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便