String, Integer, int type conversion summary, integerint

Source: Internet
Author: User

String, Integer, int type conversion summary, integerint

Today I learned the concept of encapsulation. It is very interesting to change the data type by means of automatic packing and unpacking. At the same time, mutual conversion between them is worth remembering. Of course, practice makes perfect at work, just to sort out knowledge points for your reference:

Automatic packing and automatic unpacking have been implemented since Java 7, and the conversion between int and Integer can be achieved automatically

The following is the actual operation code.

1 public class Integer04 {2 public static void main (String [] args) {3 // int ---> Integer 4 Integer i1 = Integer. valueOf (10); // return an Integer instance that represents the specified int value. 5 // create an Integer instance 6 Integer m1 = new Integer (10) through the constructor ); // encapsulation Principle 7 8 // Integer --> int 9 int i2 = i1.intValue (); // unpack 10 11 // String --> Integer12 Integer i3 = Integer. valueOf ("10"); // return an Integer instance 13 14 indicating the specified String value. // Integer --> String15 String s1 = i3.toString (); // return a specified String 16 17 // String --> int18 int i4 = Integer. parseInt ("123"); // note that it must be a pure number 19 20 // int --> String21 String s2 = 10 + ""; 22} 23}

Int packing can be achieved through Integer I = new Integer (10), or you can directly call Integer. valueOf () method implementation. You can also use this method to convert Sring to Integer. unpacking is the intValue () method called, to convert Integer to String, the toString method is called. To convert String to int, the parse () method of Integer is required, but note the following: the String must be a pure number before conversion between them. The conversion from int to String is very simple: int + String is a String, which is the basis and easy to forget.

In this case, we feel that it is helpful for our memory and understanding!

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.