Java data Boxing and exercise-sorting numbers in a string

Source: Internet
Author: User

/* Eight data types: Byte,short,int,long,float,double,char,boolean, basic data type Object wrapper class: To facilitate manipulation of the basic data type values, encapsulate them as objects, and define properties and actions that enrich the data in the object. The class used to describe the object is called the base data type Object encapsulation class. Bytebyteshortshortintintegerlonglongfloatfloatdoubledoublecharcharacterbooleanboolean basic Data type —— —— > String method: 1, Basic numeric Type + ""; 2, valueof (base type value) with static method in string class, 3, valueof (basic type Numeric) string with integer static method —— —— > Basic Data type Method: 1, using static method in wrapper class XXX Parsexxx ("xxx type string"), Eg:int I=int.parseint ("123");d ouble d=double.parsedouble ("234.56"); There are parsexxx methods above Byteshortlongfloatboolean, but character does not have this method. 2, a non-static method can be used if the string is encapsulated by an integer object. intvalue (); Converts an integer object to the base data type. Binary conversion: 1, decimal to other binary: integer.tobinarystring (integer.tooctalstring); integer.tohexstring (60); There is a more general conversion method: Integer.tostring (60,2);//Note: This method is not the same as the ToString () method in the object class,//Because this method has parameters, that is, ToString (PA   RA1,PARA2); The first parameter is the number of conversions that need to be converted, and the second parameter indicates which binary needs to be transferred. 2, other conversions to decimal: Integer.parseint ("110", 2);//Explanation: The first argument is a numeric string, and the second parameter indicates the binary of the preceding numeric value, that is, 110 is 2 binary. The result of this method returns a decimal integer type. */class wrapperdemo{public static void Main (string[] args) {int nUm=0;sop ("Integer.max_value:" +integer.max_value), SOP ("Integer.min_value:" +integer.min_value), SOP ("Integer.SIZE : "+integer.size"), SOP ("Byte.size:" +byte.size), SOP ("Byte.max_value:" +byte.max_value), SOP ("Byte.min_value:" + Byte.min_value); SOP ("Character.size:" +character.size); integer i=new integer (num); int j=23456; String Str=string.valueof (j), SOP ("str" +STR), SOP ("Binary conversion------decimal to other binary---------------"), SOP (integer.tobinarystring (60)); SOP (integer.tooctalstring), SOP (integer.tohexstring), SOP (Integer.tostring (60,2)), SOP (" The binary conversion------Other decimal---------------"), SOP (Integer.parseint (" 2 "), SOP (Integer.parseint (" ten "), SOP ( Integer.parseint ("3c", +)), SOP (Integer.parseint ("8"),//sop (Integer.parseint ("3c", 2));//This is going to go wrong. Because 3c is not a binary number integer a=new integer (123),//integer (int value) is an integer that is encapsulated as an integral object. The integer b=new integer ("123"),//integer (string s) encapsulates the int value indicated by the string parameter into an integer object. Integer c=new integer ("3c");//This is an error, because the string indicates that the value must be of type int, that is, "" must be a number. SOP ("C" +c); SOP ("A.compareto (b):" +a.compareto (b));  0 represents the same object content. 1 represents the ASCII value of a that is greater than the ASCII value of B. 1 means less than. SOP ("a" + (a+100)), SOP ("B" + (B+100)), SOP ("A==b:" + (A==b)),//== compares two objects, the object is definitely not the same.   SOP ("Equals:" +a.equals (b)); The//equals override overrides the Equals method in object. The Equals method in object is to compare two objects; the Equals method in//integer is to compare the contents of two objects. After the Integer a1=127;//jdk1.5, the auto-boxing, if the boxed is << a byte >>, then the data will be shared will not re-open space. Just like a string. Integer a2=127;//But be sure to note that this is << a byte of >>. The value range for a byte is -128~127. So when the value of A1 and A2 is 128, A1==A2 returns FALSE. SOP (A1==A2), SOP (A1.equals (A2)), Integer in=new integer ("123"),//in=in+123;//sop ("in ...: +in");//sop ( Integer.parseint (in));//Note this is wrong because the argument in parseint () must be a string of int numbers, where the argument is an integer object.  Try{int X=integer.parseint ("123"); The argument in the parseint () method must be a numeric string and cannot contain other characters. SOP (x+1);} catch (Exception e) {sop (e.tostring ());}} public static void Sop (Object obj) {System.out.println (obj);}}



/* Sort the values in a string from small to large.  "9-7" */import java.util.*;class wrappertest {private static final String space_separator= "";p ublic Static void Main (string[] args) {String str= "9-7"; int[] Arr=split (str);//split and convert to integral type//arrays.sort (arr); arr =search_sort (arr);//Sort (Ascending) SOP (arraytostring (arr)); for (int i=0;i<arr.length;i++) {SOP (arr[i]);}} /* Split string: 1, sir into a string array, 2, converts an array of type string into an integer array, i.e. parseint conversion of each element, 3, sorting an integer array, 4, converting an integer array to a string output. */public Static int[] Split (String str)//split and converted to integer array {string[] Arr=str.split (space_separator+ "+");//Note: "+" means that there may be more than one space in the middle. Int[] arr1=new int[arr.length];for (int i=0;i<arr.length;i++) {arr1[i]=integer.parseint (arr[i]);} return arr1;} public static int[] Search_sort (int[] arr)//Sort an integer array {for (int. i=0;i<arr.length-1;i++) {for (int j=i+1;j< arr.length;j++) {if (arr[i]>arr[j]) {int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}}} return arr;} public static String arraytostring (int[] arr) {StringBuffer sb=new stringbuffer (), for (int i=0;i<arr.length;i++) {if ( I!=arr.length-1) Sb.append (arr[i]+space_separator); Elsesb.append (Arr[i]);} return sb.tostring ();} public static void Sop (Object obj) {System.out.println (obj);}}


Java data Boxing and exercise-sorting numbers in a string

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.