9.2 packaging

Class structure using basic data types for packaging of
example: basic data type of package

package com.lxh.ninechapter;

public class Int {    //定义包装类
       private int data;    //包装一个基本数据类型

	public int getData() {   //从包装类中获取基本数据类型
		return data;
	}

	public void setData(int data) {    //保存基本数据类型
		this.data = data;
	}

	public Int(int data) {
		this.data = data;
	}
       
}

package com.lxh.ninechapter;

public class Java193 {
 
	public static void main(String[] args) {
		   Object obj=new Int(10);     //【装箱操作】将基本数据类型保存在包装类中
		   int x=((Int)obj).getData();  //【拆箱操作】从包装类对象中获取基本数据类型
		   System.out.println(x*2);
	}
}


The results: 20
using the Object class is the basic data type of receiver, processing parameters to achieve complete uniform.

java designed a special eight wrapper class
byte (Byte), short (Short ), int (Integer), long (Long), float (Float), double (Double), boolean (Boolean), char (Charzcter). inherit relationship as
Here Insert Picture Description
Boolean, Character: packaging an object class (Object direct subclass).
Numeric packaging (Number direct subclass): Byte, Short, Integer, Long, Float, Double.
The method defined in class Number

public byte byteValue() Byte data acquired from the wrapper class
public short shortValue() Short data acquired from the wrapper class
public abstract int intValue() Acquiring data from the wrapper class int
public abstract long longValue() Long data acquired from the wrapper class
public abstract float floatValue() Acquiring data from the wrapper class float
public abstract double doubleValue() Acquiring data from the double wrapper class

9.2.1 boxing and unboxing

  • Packing data : to save the basic data types to packaging in general, it can be accomplished using a wrapper class constructor.
    Integer class: public Integer (int value).
    Double class: public Double (double value).
    Boolean class: public Boolean (boolean value).

  • Unpacking data : acquiring basic data types from the wrapper class.
    Data packaging class defines a method of unpacking Number class.
    Boolean type: public boolean booleanValue ().
    Examples: int and Integer achieve conversion

public class nine195a {
       public static void main(String[] args) {
		      Integer obj=new Integer(10);   //装箱
		      int num=obj.intValue();         //拆箱
		      System.out.println(num*num);
	}
}

Results of: 100
This procedure utilizes the Integer class constructor provided by the basic data types packing 10, so that the basic data types become class object, then utilize class provides intValue Number () method to save the data from the wrapper class int .
Example: to achieve double and Double Conversion

public class nine195b {
       public static void main(String[] args) {
		       Double obj=new Double(2.2);
		       double dou=obj.doubleValue();
		       System.out.println(dou*2);
	}
}

The results
4.4
Examples: boolean and Boolean A Case Study

public class Nine195c {
       public static void main(String[] args) {
		      Boolean flag=new Boolean(true);
		      boolean bo=flag.booleanValue();
		      System.out.println(bo);
	}
}

Execution result: true
above operations must be performed before the JDK1.5, automatic mechanism, packaging may be performed automatically after a 1.5 numerical
writing code in the future, for the basic type of data transfer operations are recommended by packaging mechanism autoboxing to realise.
Example: int Integer and unboxing operations and automatically boxing


public class Nine196a {
       public static void main(String[] args) {
		 Integer obj=10;       //自动装箱,此时不再关心构造方法
		 int num=obj;           //自动拆箱,等价于调用了intValue()方法
		 obj++;                 //包装类对象可以直接参与计算
		 System.out.println(num+obj);
	}
}

The results: 21
autoboxing processing mechanism, directly to the basic data types Integer class number becomes the object 10 while the object directly packaging digital calculation processing.
Examples: Object receives floating-point data

public class Nine196b {
     public static void main(String[] args) {
    	  Object obj=19.2;          //double自动装箱为Double,向上转型为Object
          double num=(double)obj;    //向下转型为包装类,再自动拆箱
          System.out.println(num*2);
	}
       
}

The results: 38.4
Note: Integer autoboxing data on Comparative problem
categories object instance operation: a direct assignment A via the constructor assignment, the assignment by directly instantiated class object can be automatically packaged into the pool.
Example: to observe the operation of the pool

public class Nine197a {
        public static void main(String[] args) {
			Integer x=new Integer(10); //新空间
			Integer y=10;   //入池,直接使用
			Integer z=10;
			System.out.println(x==y);
			System.out.println(x==z);
			System.out.println(y==z);
			System.out.println(x.equals(y));
		}
}

Results of the

false
false
true
true

· Use equal between the two assignment compare () the comparison; you can also automatically assign content directly in the -128 to 127 references existing heap memory, you can use == comparison, if not within this range must rely equals to compare ().
Example: Integer equal Analyzing autoboxing

public class Nine197b {
       public static void main(String[] args) {
		    //-128~127范围内
    	   Integer numA=120;
    	   Integer numB=120;
    	   System.out.println(numA==numB);
    	    //-128~127范围内
    	   Integer num1=150;
    	   Integer num2=150;
    	   System.out.println(num1==num2);
    	   System.out.println(num1.equals(num2));
	}
}

Results of the

true
false
true

9.2.2 Data type conversion

java program will use all of the input content type String described, it is necessary to implement each of the different data types converted by the packaging, in Integer, Double, Boolean, for example, these classes will provide a corresponding static methods to achieve the conversion to pay .
(String class switching operation between the respective basic data type)

  • Integer:public static int parseInt(String s)
  • Double:public static double parseDouble(String s)
  • Boolean : public static boolean parseBoolean (String S)
    - Note: Character class does not provide conversion method
    because the string String class provides a charAt () method, you can get character at the specified index, and a character length is one bit.
    Example: int data type into a string
public class Nine198a {
       public static void main(String[] args) {
		     String str="12354";       //字符串由数字组成
		     int num=Integer.parseInt(str); //字符串转换为int
		     System.out.println(num+1);
	}
}

The results: 12,355
but in such operations, the string must be pure numbers, if there is an abnormality occurs presence of non-numeric NumberFormatException
Example: string data type becomes boolean


public class Nine198b {
       public static void main(String[] args) {
		   String strA="true";//字符串为Boolean数据形式
		   boolean flag1=Boolean.parseBoolean(strA);  //字符串转为boolean
		   System.out.println(flag1);
		   String strB="mfkermn";//任意字符串
		   boolean flag2=Boolean.parseBoolean(strB);
		   System.out.println(flag2);
       }
}

Results of the

true
false

During the conversion process, the composition of the string is true or false press requirements can be converted, if not, will be unified to false.

The basic data types into a String

Embodiment 1 : The basic data of any type string String type becomes automatically connected
Example: connecting an empty string to achieve the conversion

public class Nine199a {
        public static void main(String[] args) {
			int num=100;            //基本数据类型
			String str=num+"ll";   //字符串连接
			System.out.println(str);
		}
}

Results of: 100LL
require a separate declaration string constants, generating garbage
Method 2 : Using valueOf String class provided () method of converting
conversion method : public static String valueOf (constant data types), a plurality of times the method is overloaded

public class Nine199b {
        public static void main(String[] args) {
			int num=100;//基本数据类型
			String str=String.valueOf(num);             //字符串转换
			System.out.println(str);
		}
}

The results: 100
does not produce waste, recommended

Published 162 original articles · won praise 9 · views 3109

Guess you like

Origin blog.csdn.net/ll_j_21/article/details/104490777