“This is the seventh day of my participation in the First Challenge 2022.

preface

In logic development and programming, calculation is often used, so when Java uses numbers, it will use the basic data types used in Java for calculation, such as: Byte, int, long, double, etc. Get familiar with the Number class quickly.

Number class

The Number class is an abstract class and a superclass of the platform. The basic objects are Byte, Double, Float, Integer, Long, Short, and so on. The types under the Number class are wrapper classes that correspond one-to-one to the underlying types. The following table lists the mapping between basic types and their packaging classes. Therefore, the operation is performed after a conversion to the underlying data type. And the data type of the wrapper class is an object, which needs to be packed and unpacked.

A wrapper class Basic data types
Boolean boolean
Byte byte
Short short
Integer int
Long long
Character char
Float float
Double double
#### intValue()
  • IntValue () : Returns the specified value as an int.
  • Parameters: no
  • Return value: int
  • Example:
        Integer testNumber = 10;
        System.out.println(testNumber.intValue());
        // Output result 10
Copy the code

longValue()

  • LongValue () : Returns the specified value as long.
  • Parameters: no
  • Return value: long
  • Example:
        Integer testNumber = 10;
        System.out.println(testNumber.longValue());
        // Output result 10
Copy the code

floatValue()

  • FloatValue () : Returns the specified value as float.
  • Parameters: no
  • Return value: float
  • Example:
        Integer testNumber = 10;
        System.out.println(testNumber.floatValue());
        // The output is 10.0
Copy the code

doubleValue()

  • Function: doubleValue() : Returns the specified value as a double.
  • Parameters: no
  • Return value: double
  • Example:
        Integer testNumber = 10;
        System.out.println(testNumber.doubleValue());
        // The output is 10.0
Copy the code

byteValue()

  • Function: byteValue() : Returns a specified value in byte format.
  • Parameters: no
  • Return value: byte
  • Example:
        Integer testNumber = 10;
        System.out.println(testNumber.byteValue());
        // Output result 10
Copy the code

shortValue()

  • Action: shortValue() : Returns the specified value as a short.
  • Parameters: no
  • Return value: short
  • Example:
        Integer testNumber = 10;
        System.out.println(testNumber.shortValue());
        // Output result 10
Copy the code

compareTo()

  • CompareTo () : Compares the Number object with the parameters of the method
  • Argument: Any object
  • Return value: 0 if the specified number is equal to the argument; Returns -1 if less than; Returns 1 if less than the argument.
  • Example:

Integer i0=10;
Integer i1=10;
Integer i2=8;
Integer i3=12;
System.out.println(i1.compareTo(i0));
System.out.println(i1.compareTo(i2));
System.out.println(i1.compareTo(i3));
// The output is 0, -1, and 1 respectively
Copy the code

equals()

  • Function: The equals() method is used to determine whether the Number object is equal to the parameters of the method. Like the compareTo () method, equals() returns true and false.
  • Argument: Any object
  • Return value: Returns true if the two values being compared are equal, false otherwise.
  • Example:

Integer i0=10;
Integer i1=10;
Integer i2=8;
Integer i3=12;
System.out.println(i1.equals(i0));
System.out.println(i1.equals(i2));
System.out.println(i1.equals(i3));
// Returns the result true false false
Copy the code

The diagram below shows a simple comparison between equals() and compareTo ().

valueOf()

  • The valueOf() method is used to return the valueOf a native Number object for a given parameter, which can be a native data type, Integer, Double, or other wrapper classes
  • Parameter: integer, string, etc.; radix parameter Specifies the base number to use when parsing string S.
  • Return value: Returns the corresponding wrapper type
  • Example:

System.out.println(Integer.valueOf(10));
System.out.println(Integer.valueOf("80".16));/ / hexadecimal
System.out.println(Double.valueOf(10)); The output values are respectively10  128  10.0
Copy the code

parseInt()

  • What: The parseInt() method is used to parse string arguments as signed decimal integers.
  • Parameter: a string of base numbers 2, 8, 10, 16, etc
  • Return value: Returns the corresponding wrapper type, similar to the valueOf() method.
  • Example:

System.out.println(Integer.parseInt("10"));
System.out.println(Double.parseDouble("80"));
System.out.println(Float.parseFloat("10"));
// The output values are 10 80.0 10.0
Copy the code

conclusion

Thank you for your reading. I hope you like it. If it is helpful to you, welcome to like it. If there are shortcomings, welcome comments and corrections. See you next time.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.