1. Overview
1.概述
In this tutorial, we’ll discuss Java’s Number class. First, we’ll learn what the Number class does and what methods it contains. Then, we’ll dive into the various implementations of this abstract class.
在本教程中,我们将讨论Java的Number>c类。首先,我们将学习Number类的作用以及它包含的方法。然后,我们将深入研究这个抽象类的各种实现。
2. Number Class
2.数字类
Number is an abstract class in the java.lang package. Various subclasses extend the Number class. The most commonly used are:
Number是java.lang包中的一个bstract类。各种子类扩展了Number类。最常用的是。
- Byte
- Short
- Integer
- Long
- Double
- Float
The main purpose of this class is to provide methods to convert the numeric value in question to various primitive types such as byte, short, int, long, double, and float.
这个类的主要目的是提供将有关数值转换为各种原始类型的方法,如byte, short, int, long, double, 和float。
Four abstract methods are available to help accomplish the task:
有四个抽象方法可以帮助完成任务。
- intValue()
- longValue()
- doubleValue()
- floatValue()
Number also has two concrete methods, byteValue() and shortValue(), which will return the byte value and short value of a specified number, respectively. To learn more about the different implementations of the Number class, please refer to our article on Wrapper Classes.
Number也有两个具体方法,byteValue()和shortValue(),它们将分别返回指定数字的byte值和short值。要了解更多关于Number类的不同实现,请参考我们关于Wrapper Classes的文章。
In the upcoming sections, we’ll learn more about these methods and their usage.
在接下来的章节中,我们将进一步了解这些方法和它们的用法。
3. Concrete Methods
3.混凝土方法
Let’s discuss the concrete methods one by one.
让我们逐一讨论这些具体方法。
3.1. shortValue()
3.1.shortValue()
As the name suggests, this method converts the specified Number object into a primitive short value.
顾名思义,该方法将指定的Number对象转换为一个原始的short值。
The default implementation casts the int value into short and returns it. However, subclasses have their own implementations, and they cast respective values into short and then return.
默认的实现是将int值转换为short并返回。然而,子类有它们自己的实现,它们将各自的值转换为short,然后返回。
Here is how a Double value is converted into a short primitive type:
下面是一个Double值如何被转换成short原始类型。
@Test
public void givenDoubleValue_whenShortValueUsed_thenShortValueReturned() {
Double doubleValue = Double.valueOf(9999.999);
assertEquals(9999, doubleValue.shortValue());
}
3.2. byteValue()
3.2.byteValue()
This method returns the value of the specified Number object as a byte value. Nevertheless, child classes of the Number class have their own implementations.
该方法将指定的Number对象的值作为byte值返回。然而,Number类的子类有自己的实现。
Here’s how a Float value can be converted into a byte value:
下面是如何将Float值转换为byte值。
@Test
public void givenFloatValue_whenByteValueUsed_thenByteValueReturned() {
Float floatValue = Float.valueOf(101.99F);
assertEquals(101, floatValue.byteValue());
}
4. Abstract Methods
4.抽象方法
Additionally, the Number class also has a few abstract methods and several subclasses that implement them.
此外,Number类也有一些抽象方法和几个实现它们的子类。
In this section, let’s have a quick look at how these methods are used.
在本节中,让我们快速了解一下这些方法是如何使用的。
4.1. intValue()
4.1.intValue()
This method returns the int representation of the Number in context.
该方法返回上下文中Number的int表示。
Let’s see how a Long value can be changed into int:
让我们看看如何将一个Long值变为int。
@Test
public void givenLongValue_whenInitValueUsed_thenInitValueReturned() {
Long longValue = Long.valueOf(1000L);
assertEquals(1000, longValue.intValue());
}
Certainly, the compiler is performing a narrowing operation here by converting a long value into an int.
当然,编译器在这里通过将long值转换为int来执行窄化操作。
4.2. longValue()
4.2.longValue()
This method will return the value of the Number specified as a long.
该方法将返回Number指定的long的值。
In this example, we see how an Integer value is converted into a long via the Integer class:
在这个例子中,我们看到一个Integer值是如何通过Integer类转换为long。
@Test
public void givenIntegerValue_whenLongValueUsed_thenLongValueReturned() {
Integer integerValue = Integer.valueOf(100);
assertEquals(100, integerValue.longValue());
}
In contrast to the intValue() method, longValue() returns the long value after a widening primitive conversion.
与intValue()方法相反,longValue()在widening原始转换之后返回long值。
4.3. floatValue()
4.3.floatValue()
We can use this method to return the value of the specified Number as a float. Let’s take a look at how a Short value can be converted into a float value:
我们可以使用这个方法将指定的Number的值作为float返回。让我们来看看如何将一个Short值转换成float值。
@Test
public void givenShortValue_whenFloatValueUsed_thenFloatValueReturned() {
Short shortValue = Short.valueOf(127);
assertEquals(127.0F, shortValue.floatValue(), 0);
}
Likewise, longValue() and floatValue() also perform a widening primitive conversion.
同样,longValue()和floatValue()也进行了加宽的原始转换。
4.4. doubleValue()
4.4.doubleValue()
Finally, this method converts the value of the given Number class to the double primitive data type and returns it.
最后,这个方法将给定的Number类的值转换为double原始数据类型并返回。
Here is an example of using this method to convert a Byte to double:
下面是一个使用此方法将Byte转换为double的例子。
@Test
public void givenByteValue_whenDoubleValueUsed_thenDoubleValueReturned() {
Byte byteValue = Byte.valueOf(120);
assertEquals(120.0, byteValue.doubleValue(), 0);
}
5. Conclusion
5.总结
In this quick tutorial, we took a look at some of the most important methods in the Number class.
在这个快速教程中,我们看了一下Number 类中一些最重要的方法。
Finally, we’ve demonstrated how these methods can be used in various Wrapper classes.
最后,我们演示了如何在各种Wrapper类中使用这些方法。
As always, the full source code of the article is available over on GitHub.
一如既往,文章的完整源代码可在GitHub上获得over。