Java Learning Lesson 32nd (Common Object API)-Basic data type Object wrapper class

Source: Internet
Author: User

Set the base data type (8 types: int). The benefit of encapsulating an object is that it can encapsulate more functions and methods in the object to manipulate the data

A common operation is to use a conversion between a basic data type and a string

Basic data type Object wrapper classes are typically used for conversions between basic types and strings

Basic type-----> string

1. Basic type value + ""

2. Using the valueof (base type value) in the String class

3. Use integer.valueof ();

String--Basic type numeric

1. Use the static method in the wrapper class XXX parsexxx ("XXXX") such as: int parseint ("123")/boolean Parseboolean ("true"), but character has no parse, superfluous

2. If the string is encapsulated by an integer object

A non-static method can be used Intvalue ()

Convert an Integer object to a base type value

(The string is encapsulated into an object before being converted with Xxxvalue)

public class Main {public static void main (string[] args) {/* * Basic Data type wrapper class * To facilitate the base data type value, encapsulate it as an object, define the properties in the object and manipulate the behavior that enriches the data * for The class that describes the object is the basic data type wrapper class *  * byte byte * short Shrot * int Integer * Long long * Double double * Char Character * Boolean Bo Olean *  */int num; System.out.println (integer.max_value);//int Maximum 2147483647system.out.println (integer.tobinarystring (6));// Convert to binary integer x = new Integer ("4"),//system.out.println (x), int xx = Integer.parseint ("1234");//convert string to int, And the parseint method is static System.out.println (XX), if (Boolean.parseboolean ("true")) {integer Xinteger = new Integer ("123");< Span style= "White-space:pre" ></span>system.out.println (Xinteger.intvalue ());//Convert "123" to int}}

Binary conversion

public class Main {public static void main (string[] args) {/* decimal and other */system.out.println (Integer.tobinarystring (6 );//Binary System.out.println (integer.tooctalstring (10));//octal System.out.println (integer.tohexstring (20));// Hex System.out.println (integer.tostring (50, 4));//50 in the representation of the binary/* other  decimal */system.out.println ( Integer.parseint ("110", 2));//110 is considered a binary representation and converted to a decimal System.out.println (Integer.parseint ("3C", 16));}}

Automatic packing and unpacking

public class Main {public static void main (string[] args) {integer a = new Integer ("3"); integer b = new Integer (3); System.out.println (A = = B);//Two new addresses must be different System.out.println (A.equals (b));//equals compares content System.out.println ( A.compareto (b)); int num = 5;num++;integer Xinteger = 5;//xinteger = new Integer (5), auto boxing Xinteger++;//xinteger = Xinteg ER + 1   actual operation   Xinteger = new Integer (Xinteger.intvalue () +1)//Note exception// After JDK1.5, automatic boxing, if the boxing is a byte, then the data will be shared, will not re-open space Integer x = 128;integer y = 128; SYSTEM.OUT.PRINTLN (x = = y);//128 before true,128 and 128 is falseSystem.out.println (X.equals (y));//true}}

Practice:

Import java.util.arrays;/* * Sort the values in a string from small to large * "123 2 5 652 7-3 4 456 456 489" */public class Main {private static  Final String Sps_sigal = "";//ctrl+shift+x lowercase to uppercase, uppercase to lowercase +ypublic static void main (string[] args) {String str = "123 2 5 652 7-3 4 456 456 12 489 "; string[] arr = StringToArray (str); int aa[] = Stringtoint (arr); sort (aa); StringBuilder string = inttostring (AA); show (string);} public static StringBuilder inttostring (int[] arr) {StringBuilder SB = new StringBuilder (), for (int i = 0; i < Arr.leng Th i++) {if (i = = arr.length-1) sb.append (Arr[i]); Elsesb.append (Arr[i] + ",");} return SB;} public static int[] Stringtoint (string[] arr) {int aa[] = new Int[arr.length];for (int i = 0; i < arr.length; i++) {aa[ I] = Integer.parseint (Arr[i]);} return AA;} public static string[] StringToArray (String str) {string[] arr = Str.split (Sps_sigal); return arr;}  public static void sort (int[] arr) {arrays.sort (arr);//Arrays class is provided with the Sort method/* * for (int i = 0;i<arr.length-1;i++) {for (int j = * 0;j<arr.length-1-i;j++) {if (Arr[j] > Arr[j+1]) swap (arr,j,j+1);}} */}public static void Swap (int[] arr, int i, int j) {int t = arr[i];arr[i] = arr[j];arr[j] = t;} public static void Show (StringBuilder string) {System.out.println (string);}}



Java Learning Lesson 32nd (Common Object API)-Basic data type Object wrapper 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.