Java-Digital Class

Source: Internet
Author: User

For 6 basic numeric types, Java provides classes that correspond to each other. Number class and subclass of six subdivisions.

Note: Number In addition to these subclasses, there are four subclasses, of which BigDecimal and BigInteger are used for high-precision computations, while Atomicinteger and Atomiclong are used for multithreaded programs and are not introduced again.

Java's numeric classes provide functionality, such as individual numeric classes that define constants such as maximums, minimums, and so on, and they also contain conversions that convert basic data types to strings, as well as each binary.

The following table class is in addition to some of the methods common to number subclasses:

method description
byte Bytevalue ( )
Short shortvalue ()
int intvalue ()
Long longvalue ()
Float floatvalue ()
Double doublevalue ()

Converts the numeric object to the base number type and returns

int CompareTo (Byte anot herbyte)
int compareTo (Double anotherdouble)
int compareTo (Float anotherfloat)
int CompareTo (Integer Anotherinteger)
int compareTo (Long anotherlong)
int compareTo (short anothershort)

Compares the numeric object to the parameter

Boolean equals (Object obj)

Determines whether the numeric object is equal to the parameter, Returns true if the type of the parameter is equal to the value and the number object, as well as additional requirements for double and float types, as described in the API documentation

Each number class also contains some methods for converting between strings and the various carry systems, as an example of the following examples:

Method Description
static Integer decode(String s) Converts a string into an integer object, with the form of a string that allows decimal (default), octal (starting with 0), and hexadecimal (starting with 0x).
static int parseInt(String s) Returns an integer (decimal).
static int parseInt(String s, int radix) Returns an integer that converts a string (representing the Carry radix system, 10, 2, 8, or 16, respectively, for decimal, binary, 8, and 16 binary).
String toString() Returns a String representing the number
static String toString(int i) Returns a String representing an integer of the specified underlying type
static Integer valueOf(int i) Returns an integer class that corresponds to integers of the specified underlying type
static Integer valueOf(String s) Returns an integer class converted by a string that must be represented in decimal
static Integer valueOf(String s, int radix) Returns an integer class with string conversions, and the second parameter specifies a carry system.

Digital output format

The printf and format methods that utilize PrintStream (System.out is a PrintStream object) can output strings and numbers together. Its Print method and println method can only output a single object.

The printf and format methods are the same, using the syntax:

Format (format, Object ...)

Where the first parameter, format, specifies the form of the output, which can then be followed by several output objects, using an example:

int i = 461012; System.out.format ("The value of I is:%d%n", i);

The output format string consists of a literal and a format identifier, a format identifier starting with%, ending with a converted character, a character that indicates a data type, and a format identifier that can have other tags and identifiers in the middle, as described in java.util.Formatter .

In addition, java.text.DecimalFormat classes can also be used to control the start and end of numbers in 0, prefix, grouping, and decimal points.

For numbers, Java provides Math classes for advanced book-ordering operations such as absolute value, trigonometric functions, Gong, and so on.

In addition the math class provides a ramdom method for generating a random number:

// 0.0 <= Math.random () < 1.0, so the following sentence will produce a 0-10 random integer int number = (int) (Math.random () * 10);

If you need to generate a series of random numbers, you can alsojava.util.Random

Java-Digital Class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.