Java Note 9

Source: Internet
Author: User

Java Note 9
Data Type PackagingReprinted please indicate Source: http://blog.csdn.net/u012637501 (embedded _ small J of the sky)
I. OverviewJava provides both simple types of basic data and corresponding packaging classes for data. The basic simple data type can improve the system performance and meet the needs of most applications. However, simple types do not have the characteristics of objects and cannot meet certain special requirements. For example, in Java, the parameter types of many methods are objects. For example, to wrap integers, we can use Integer objects to easily convert integers and strings.
Ii. Integer class (java. lang. Object-> java. lang. Number)The value of the int object of the original type encapsulated by the integer class. An integer type object contains the int type of a field. This packaging class provides several methods to convert integer to String or String to integer data.(1) constructor

Integer(Int value): creates an Integer object whose content is an Integer value.
Integer(String s): creates an Integer object whose content is a String specified by a Sring object (for example, Integer ("123 "))
(2) common member functions a.int/long/float/double int/long/float/doubleValue (): converts the Integer data corresponding to an Integer object to the int/long/float/double basic data type. B. static Integer decode (String nm): decodes a String object into an Integer object c.int hashCode (): returns the hash value d of the Integer object. static int parseInt/parseUnsignedInt (String s /...): string type... convert to integer data e. static int sum (int a, int B): calculates the sum of a and B of Two integer data types. string toString (): Convert the integer value to a String object g. static Integer valueOf (int I): obtains the Integer value h corresponding to the Integer object. static Integer valueOf (String s): returns an Integer object whose Integer value is a specified String. 3. Application Instances 1. convert a string to an integer.Method 1: int w = Integer. parseInt ("0"); get the ASCII code of "0" = 48 = w; Method 2: int h = new Integer ("0 "). intValue (); the value of the Integer object is obtained. The return type is int type. Method 3: int h = Integer. valueOf ("0 "). intValue (); the effect is the same as method 2, but the method 3 calls the Integer static method valueOf to return the Integer object Summary: implement the string to the basic data type (such as Integer) int w = Xxx packaging class. parseXxx (String str), such as int w = Integer. parseInt ("0"), where parseXxx () is the static method of the packaging class. 2. Convert the packaging class into basic dataInteger I = new Integer ("0"); int h = I. intValue (); summary: basic data type = Xxxx packaging Class Object. XxxValue; 3. Source Code: A program prints a rectangle composed of an asterisk (*) on the screen. The width and height of the rectangle are specified by parameters passed during the program running.
Class TestInteger {public static void main (String [] agrs) {// 1. converts the input character to an Integer int h = Integer. parseInt (agrs [0]); // The height of the rectangle int w = new Integer (). intValue (agrs [1]); // width of the rectangle // 2. use StringBuffer to create a variable array space and store a row * for (int I = 0; I 

Reference: http://docs.oracle.com/javase/8/docs/api/index.html

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.