Java serialization 77 Integer common methods, Integer, int, String mutual conversion, automatic packing, automatic unpacking

Posted by suvarthi on Wed, 22 Jan 2020 16:23:35 +0100

1, On the common methods in Integer

 

package com.bjpowernode.java_learning;

​

public class D77_1_ {

  public static void main(String[] args) {

    Integer i1 = new Integer(10);

    //take Integer Type to int type

    int i2 = i1.intValue();

    System.out.println(i2);

    //Important: static int parseInt(String s)String to number

    int age = Integer.parseInt("25");

    System.out.println(age);

    //int price = Integer.parseInt("abe");//This statement will be programmed successfully, but there is a problem in the operation. Parameters can only accept numbers

   

    //Important: static double parseDouble(String s)

    double d1 = Double.parseDouble("3");

    System.out.println(d1);

   

    //Static method: static String toBinaryString(int i)Put an integer i Convert to (string) binary return

    //static String toHexString(int i)Convert to hex

    //static String toOctalString(int i)Convert to octal

    System.out.println(Integer.toHexString(90));

    System.out.println(Integer.toBinaryString(89));

    System.out.println(Integer.toOctalString(89));

   

    //Will one int Type or String Type to Integer Type: two methods, direct initialization; use Integer.valueOf()Method

    System.out.println(Integer.valueOf("45"));

    System.out.println(Integer.valueOf(45));

    System.out.println("=================================="); 

  }

}

2.Integer\int\String three types of mutual conversion

 

    //int->Integer

    Integer i5 = Integer.valueOf(10);

   

    //Integer->int

    int i6 = i5.intValue();

   

    //String ->Integer

    Integer i7 = Integer.valueOf("10");

   

    //Integer ->String

    String s5 = i5.toString();

   

    //String -> int

    int i8 = Integer.parseInt("10");

   

    //int -> String

String s6 = 10 + "";

 

 

2, Automatic packing and unpacking

1. New features of jdk5.0

The following features are available after JDK 1.5, including 1.5,

 

package com.bjpowernode.java_learning;

​

public class D77_2_EncasementAutomatically {

  public static void main(String[] args) {

    //JDK5.0 Previous

    //int->Integer  Boxing

    Integer i1 = new Integer(10);

    //Integer ->int  Unpacking

    int i2 = i1.intValue();

   

    //JDK5.0 After, including 5.0

    Integer i3 = 10;//Automatic boxing

    int i4 = i3;//Automatic dismantling

    System.out.println(i3);

    System.out.println(i4);

   

    m1(445);//Automatic boxing

    System.out.println(m2(85,50));//First automatic packing and then automatic unpacking

  }

  public static void m1(Object o) {

    System.out.println(o);

  }

  public static int m2(Integer i1,Integer i2) {

    return i1-i2;

  }

​

}

2. In depth automatic packing and unpacking

(1) Automatic packing and unpacking is a concept in the compiling stage of a program, which is independent of the running of the program;

(2) The main purpose of automatic packing and unpacking is to facilitate programming. ​

3, Source code:

D77_1_IntegerAndIntAndStringTransform.java

D77_2_EncasementAutomatically.java

https://github.com/ruigege66/Java/blob/masterD77_1_IntegerAndIntAndStringTransform.java

https://github.com/ruigege66/Java/blob/master/D77_2_EncasementAutomatically.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to pay attention to WeChat public number: Fourier transform, personal public number, only for learning exchanges, background reply "gift package", access to big data learning materials.

Topics: Java github JDK Programming