Java Basics (iv): Java number & Math class, Character class, String class

Source: Internet
Author: User
Tags square root

First, Java number & Math class:

1. Number class:

  Generally, when you need to use numbers, we usually use built-in data types, such as: Byte, int, long, double, and so on. However, in the actual development process, we often encounter situations where objects need to be used rather than built-in data types. To solve this problem, the Java language provides the corresponding wrapper class for each built-in data type. All wrapper classes (Integer, Long, Byte, Double, Float, short) are subclasses of the abstract class number.

This kind of wrapper, specifically supported by the compiler, is called boxing, so when the built-in data type is used as an object, the compiler will box the built-in type into a wrapper class. Similarly, the compiler can also disassemble an object into a built-in type. The number class belongs to the Java.lang package.

  Integer x = 5; when x is assigned an integer value, because X is an object, the compiler is boxing the X.

x = x + 10; Then, to allow X to be added, the X is disassembled.

  Automatic boxing and unpacking in Java:

/** * Automatic boxing and unpacking in Java * Simply put, boxing is the automatic conversion of the base data type to the wrapper type; unpacking is the automatic conversion of the wrapper type to the base data type . */ Public classNumber { Public Static voidMain (string[] args) {/** Integer i1 = 128;        packing , equivalent to integer.valueof (+); int t = I1;        Equivalent to i1.intvalue () unpacking System.out.println (t); */        /** For values between –128 to 127 (by default, 127), it is boxed and will be reused in memory, but if this value is exceeded, the system will re-new an object */Integer I1= $; Integer I2= $; /** Note = = and equals = = It compares the object's address Equlas compared to the object's contents */        if(i1==I2) {System. out. println ("true"); } Else{System. out. println ("false"); }    }}

2. Math class:

Java's math contains properties and methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots, and trigonometric functions. The math method is defined as the static form, which can be called directly from the main function through the math class.

3, Number & Math class method:

Xxxvalue (): Converts the Number object to the value of the XXX data type and returns. For example: Floatvalue (): Returns the specified value in float form

CompareTo (): Compares a number object to a parameter

Equals (): Determines whether the number object is equal to the parameter

ValueOf (): Returns the built-in data type specified by a number object

ToString (): Returns a value as a string

parseint (): Resolves a string to an int type

ABS (): Returns the absolute value of the parameter

Ceil ()/floor (): Rounding left/right for shaping variable, return type double

Rint ()/round (): Returns the integer closest to the parameter, return type double/returns the nearest int, Long value

Min ()/max (): Returns the minimum/maximum value in two parameters

exp ()/log (): Returns the number of arguments of the base e of natural numbers/the pairs of values of the natural number of the return parameter

Pow ()/sqrt (): Returns the second parameter of the first argument to the square root of a parameter

Sin ()/cos ()/tan ()/asin ()/acos ()/atan (): Specifies the sine/cosine/tangent of a double type argument

Todegrees ()/toradians (): Return angle/radians

Random (): Returns the stochastic number

two , Character class:

  The Character class is used to manipulate a single character, and the Character class wraps the value of a primitive type char in an object:

CharCH ='a';//Unicode Character RepresentationCharUnichar ='\u039a'; //character ArrayChar[] Chararray ={'a','b','C','D','e'};

The wrapper class character class is provided for the built-in data type char. The character class provides a series of methods to manipulate characters. You can create a Character class object using Character's construction method, for example:Character ch = new Character(' A');

In some cases, the Java compiler automatically creates a character object. For example, when a parameter of type char is passed to a method that requires a character type parameter, the compiler automatically converts the char type parameter to the character object. This feature is called boxing, which in turn is called unpacking.

// the original character ' a ' is boxed into the Character object ch ' a ' ; // The original character ' X ' is boxed with the test method and returns the value of the unpacking to ' C ' char c = Test ('x');

Methods of the character class:

Isletter (): Whether it is a letter

Isdigits (): Whether it is a numeric character

Iswhitespace (): Whether it is a space

ToString (): Returns the string form of a character with a string length of only 1

Third, String class:

Java Basics (iv): Java number & Math class, Character class, String class

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.