Invoke the original type and type
Abstract: invoke the original type and type
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="261" height="86" align="center" valign="top"> </ td> <td width="423" valign="top">
Java offers two different types: type and use the original type (or built-in type). In addition, Java is also provided for each type of the original package category (Wrapper).
If you need an integer variable is the use of basic int type it, or to use an object such as Integer? If you need to declare a boolean type, the use of basic boolean, or the use of Boolean type of an object? This paper will help you make a decision.
Below is a list of the original object types as well as their packaging category. î—± î—±
î—± î—± î—± primitive type of package î—± î—± boolean î—± î—± Boolean
î—± î—± char î—± î—± î—± î—± Character
î—± î—± byte î—± î—± î—± î—± Byte
î—± î—± short î—± î—± î—± î—± Short
î—± î—± int î—± î—± î—± î—± î—± Integer
î—± î—± long î—± î—± î—± î—± Long
î—± î—± float î—± î—± î—± î—± Float
î—± î—± double î—± î—± î—± Double
î—± invoke the original type and type of conduct is totally different, and they have different semantics. For example, assume that there are two methods of a local variable, a variable of type int original, and the other variable is the object of an Integer object quote: î—±
î—± int i = 5; / / primitive type
î—± î—± Integer j = new Integer (10); / / object reference
î—± î—± these two variables are stored in the local variables in the table, and a few are in the Java stack operation in the operation, but they said it's completely different. (This article in the following sections will be used to replace the generic term stack operation of stack or local variables table.)
Int type and the original object reference each stack of 32. (To express an int or an object reference, the Java Virtual Machine to use at least 32 storage.) Integer object of the stack is not the object itself, but an object reference. Java to all objects in the object invoked visit. Object reference refers to the object storage pile of a regional indicator. When a primitive type of statement, the statement itself for the type of storage.
î—± î—± invoke the original type and the characteristics of different types and usage, including: size and speed to the type of data structure storage, when the decision to invoke the original type and type for a particular instance of the class specified in the data missing Province value. Object variables cited examples of the default values for null, and the original type of instance variables with the default values of their types.
î—± î—± many procedural code will also contain the original object types, as well as their packaging. When they check whether the same at the same time the use of these two types and the correct understanding of how they interact and coexistence will be a problem. Programmers need to understand how these two types of work and interaction in order to avoid error code. î—±
î—±, for example, not the original type of call, but the call can be targeted:
î—± î—± int j = 5;
î—± î—± j.hashCode (); / / error
î—± î—± //…
î—± î—± Integer i = new Integer (5);
î—± î—± i.hashCode (); / / correct î—± î—±
Not the original call new type, there is no need to create objects. This savings in time and space. The mixed use primitive types, and objects may also lead to accidents with the results of the assignment. Did not appear to have the wrong code may not be able to complete the work you want to do. For example:
î—± î—± import java.awt.Point;
î—± î—± class Assign
î—± î—± (
î—± î—± î—± public static void main (String args [])
î—± î—± î—± (
î—± î—± int a = 1;
î—± î—± int b = 2;
î—± î—± Point x = new Point (0,0);
î—± î—± Point y = new Point (1,1); / / 1
î—± î—± System.out.println ( "a is the" + a);
î—± î—± System.out.println ( "b is" + b);
î—± î—± System.out.println ( "x is" + x);
î—± î—± System.out.println ( "y is" + y);
î—± î—± System.out.println ( "Performing assignment and" + "setLocation …");
î—± î—± a = b;
î—± î—± a + +;
î—± î—± x = y; / / 2
î—± î—± x.setLocation (5,5); / / 3
î—± î—± System.out.println ( "a is the" + a);
î—± î—± System.out.println ( "b is" + b);
î—± î—± System.out.println ( "x is" + x);
î—± î—± System.out.println ( "y is" + y);
î—± î—± î—±)
î—± î—± î—±)
î—± This code generates the following output:
î—± î—± a is a
î—± î—± b is 2
î—± î—± x is java.awt.Point [x = 0, y = 0]
î—± î—± y is java.awt.Point [x = 1, y = 1]
î—± î—± Performing assignment and setLocation …
î—± î—± a is 3
î—± î—± b is 2
î—± î—± x is java.awt.Point [x = 5, y = 5]
î—± î—± y is java.awt.Point [x = 5, y = 5] î—±
î—± integers a and b to amend the results of the local no accident. B is the value of a given integer variables, a result of the increased value of 1. This output reflects our hope that happening. However, it is surprising, in the assignment and call setLocation after x and y object output. We have completed x = y assignment after a specially called setLocation the x, x and y values how will be the same? After all, we will be given x y, then x changes, which we rounded a and b of the operation no different. î—± î—±
The confusion is from the original type and the use of the object. These two types of assignment of the role played by little different. However, it may seem all different. Assigned to equal (=) equivalent to the value of the left side of the right values. This is the original type (such as in front of int a and b) is obvious. For non-primitive types (such as Point object), the assignment is to amend object reference, and not object itself. Therefore, in the words x = y;, x is y. In other words, because the x and y is the object reference, they are now invoked with an object. Therefore, the x will be any changes made changes y.
î—± î—± because x and y invoked with an object, so the implementation of the x and the y all means of implementation methods role on the same object.
î—± î—± distinction invoked the original type and type and use the semantic understanding is very important. If this is not done, then the code will not complete the preparation of the work scheduled.
</ Td> </ tr> <tr> ↑ Back






