java-Boxing/unpacking-string conversion to basic data type

Source: Internet
Author: User

I. Understanding the meanings and types of packages in Java

Java is an object-oriented programming, that is, everything is an object, then there is a contradiction, from the data to know that the data in Java is divided into basic data types and reference data types, but how the basic data type is an object? At this point, the basic data types need to be wrapped, the Java 8 basic data type into a class form, then this is the role of the wrapper class.

The relationship between the wrapper class and the base data type is as follows:

    

1, Integer, Byte, Float, Double, short, long all belong to the subclass of number, and numbers itself provides a series of operations that return the basic data types above 6

2, character belongs to the direct sub-class of object

3. Boolean belongs to the direct subclass of object

  

Second, packing and unpacking

What is packing and unpacking?

Boxing: Changing the base data type into a wrapper class----boxing operation

Unpacking: The process of changing a wrapper class into a basic data type---unpacking operation

 class   object02 { public  static  void   main (string[] args) {int  x[] = {1,2,3,4,5}; Integer w  = new  Integer (x[0]); //  boxing          System.out.println (w);   int  temp = W.intvalue (); //  unpacking      System.out.println (temp); }}
Manual packing/unpacking operation based on JDK1.5
publicclass Object03 { publicstaticvoid main (string[] args) { float f = 3.14f; =new// The floating-point data type is boxed System.out.println (x ); float // Unpacking System.out.println (y); }}

Automatic boxing and unpacking operations 

 public  class   Object04 { static  void   main (string[] args) {intege    R x  = 23; //        /Custom boxing for shaping    Float y = 3.14f; //        /automatically boxed as floating-point type  int          Y=y; //         unpacking an int assigns a value to x  in the boxed element float     X=x; //  unpacking to float assigns a value to x  in a boxed element         System.out.println (X);    System.out.println (Y); }}

Three, string conversion to basic data type

Integer. parseint (str): Converts the string "str" to an integral type

Float. parsefloat (f): Converts a number with a decimal character to a floating-point type

Note: When a string is converted to a base data type, the string must be a number, or a program error appears

 Public classObject05 { Public Static  voidMain (string[] args) {String str= "234";//a number that declares a stringString f = "3.14"; intx = Integer.parseint (str);//Change a string to reshape        floatFF = Float.parsefloat (f);//turning a string into a floating-point classSYSTEM.OUT.PRINTLN ("The product of two integers is:" +x+ "*" +x+ "=" + (x*x)); System.out.println (The product of the two decimals is: "+ff+" * "+ff+" = "+ (ff*ff)); }}//the product of two integers is: 234*234=54756//two decimal products are: 3.14*3.14=9.859601

java-Boxing/unpacking-string conversion to basic data type

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.