The automatic boxing and unpacking feature in Java.

Source: Internet
Author: User

1 //automatic boxing and unpacking functionality in Java.2 classautoboxingunboxing3 {4      Public Static voidMain (string[] args)5     {6         //assigns a basic type variable directly to the Interger object. 7Integer inobj = 5;8Object inObj1 = 6;9         //assign a Boolean variable directly to a variable of type object.TenObject boolobj =true; One         //assigning an Integer object directly to the INT type variable A         intit =inobj; -         if(BoolobjinstanceofBoolean) -         { the             Booleanb =(Boolean) boolobj; - System.out.println (b); -         } -  +         if(inObj1instanceofInteger) -         { +             intx =(Integer) inObj1; ASystem.out.println ("x=" +x); at         } -     } -}
1 //the wrapper class implements the conversion between the base type and the string.2 /*3 "1" There are two ways to convert the value of a string type to a value of a base type:4 (1) The parsexxx (String s) static method provided by the wrapper class (this method is provided by all wrapper classes except character). 5 (2) The xxx (String s) constructor provided by the wrapper class. 6 the "2" string class provides multiple (9) overloaded valueof () methods for converting a primitive type variable to a string. 7 If you want to convert a primitive type variable to a string, there is an easier way to do this: set the base type variable and the ""8 for the connection operation, the system automatically converts the basic type variable into a string. 9 */Ten  Public classprimitive2string One { A      Public Static voidMain (string[] args) -     { -         //(1) theString intstr = "123"; -         //converts a specific string into an int variable.  -         intIt1 =Integer.parseint (INTSTR); -         intIt2 =NewInteger (INTSTR); + System.out.println (it2); -  +         //(2) AString floatstr = "4.56"; at         //convert a specific string into a float variable -         floatFT1 =float.parsefloat (FLOATSTR); -         floatFT2 =NewFloat (FLOATSTR); - System.out.println (ft2); -  -  in         //(3) -         //Convert a float variable to a string variable to         floatF1 = 2.345f; +String Ftstr =string.valueof (F1); - System.out.println (FTSTR); the  *         //(4) $         //convert a double variable to a string variablePanax Notoginseng         DoubleD1 = 1.234; -String Dbstr =string.valueof (d1); the System.out.println (DBSTR); +  A         //(5) the         //Convert a Boolean variable to a string variable +         BooleanL =true; -String Boolstr =string.valueof (l); $ System.out.println (Boolstr.touppercase ()); $  -         //(6) -         //The basic type variable is converted to a string type by the join operation. the         intx = 34; -String xx = 34+ "";WuyiSystem.out.println ("This is a string number" +xx); the  -         //the CompareTo () method in the String class WuInteger I1 = 4; -         intX1 = I1.compareto (NewInteger (5)); AboutSystem.out.println ("x=" +x1); $     } -}

The automatic boxing and unpacking feature in Java.

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.