Java learning--number class, character class

Source: Internet
Author: User

Number class

When working with numbers, we typically use built-in data types, such as

int a = 9; float B = 3.14

In actual development, however, we often encounter objects that need to use objects rather than built-in data types. To solve this problem, Java provides a wrapper class for each built-in data type (such as using functions owned by that type), such as:

This package, specially supported by the compiler, becomes boxed,

Example

 Public class fortest {    publicstaticvoid  main (String args[]) {        // char grade = Args[0].charat (0);        Integer x = 5;         + +;        SYSTEM.OUT.PRINTLN (x);    }}

Integer x = 5, the compiler boxed X, and the x+=20 compiler removed the x.

The number method uses an example

 Public classFortest { Public Static voidMain (String args[]) {//char grade = Args[0].charat (0);Integer x = 5;        SYSTEM.OUT.PRINTLN (x); System.out.println ("5 CompareTo 1:" + x.compareto (1)); System.out.println ("5 compareTo:" + x.compareto (50)); Integer Intx= Integer.valueof ("3"); System.out.println ("ValueOf" +intx); //The object holds the value extracted from the specified String when parsing with the cardinality provided by the second argumentInteger inty = integer.valueof ("13", 16); System.out.println ("ValueOf" +inty); System.out.println ("Math.max (3,5):" + Math.max (3, 5)); System.out.println ("Math.min (3,5):" + math.min (3, 5)); System.out.println ("Exp (2):" + math.exp (2)); System.out.println ("Log (2):" + Math.log (2)); System.out.println ("Pow (2,3):" + Math.pow (2, 3)); System.out.println ("sqrt (2,3):" + math.sqrt (4)); System.out.println ("Toradians (+):" + Math.toradians (30)); System.out.println ("Sin (1):" + Math.sin (Math.toradians (30))); System.out.println ("Random:" +math.random ()); }}

Run results

CompareTo compareTo 50-1Valueof3valueof19math.max (3,5): 5math.min (3,5): 3exp (2) : 7.38905609893065log (2): 0.6931471805599453pow (2,3): 8.0sqrt (2,3): 2.0Toradians ( : 0.5235987755982988sin (1): 0.49999999999999994random:0.10509218688109245
Character class

The character class is used to manipulate a single character process, such as

    char ch = ' a ';     // Unicode character Representation    char unichar = ' \u039a ';     Char [] Chararray = {' A ', ' B ', ' C '};

However, in real-world development, objects that use char types are often encountered, rather than built-in data types. The compiler automatically converts the char type to a character object to be "boxed", which in turn becomes "unboxing." Character usage examples

 Public classChartest { Public Static voidMain (String args[]) {Charch = ' A '; //Unicode Character Representation        CharUnichar = ' \u039a '; Char[] Chararray = {' A ', ' B ', ' C '}; Character CH2= ' A '; System.out.println ("A is a letter:" +Character.isletter (CH2)); System.out.println ("A is a numeric character:" +character.isdigit (CH2)); System.out.println ("is a space:" +Character.iswhitespace (CH2)); System.out.println ("\\t is a space:" + character.iswhitespace (' \ t ')); System.out.println ("A is an uppercase letter:" +character.isuppercase (CH2)); System.out.println ("A is a lowercase letter:" +character.islowercase (CH2)); System.out.println ("A translates to uppercase:" +character.touppercase (CH2)); System.out.println ("A translates to lowercase letters:" +character.tolowercase (CH2)); System.out.println ("A character converted to a string:" +character.tostring (CH2)); }}

Run results

A is a letter:trueA is a numeric character:false Ifit is a space:false\ t is a space:  TrueA is an uppercase letter:falseA is a lowercase parent:truea converts to uppercase letters: AA translates to lowercase letters: The AA character is converted to a string: a

Java learning--number class, character 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.