I saw O'relly's book on one day, and suddenly there is a inexplicable impulse, I am also very technical writer.
Technical writers must naturally have difficult technology, but their own technology is really unable to enter the master. Only with yourself exercises, you can do this far more ideal.
This time I published an article to try I can do it. If a expert can't see it, I am talking alone here, you can refer to criticism (it is best to give a little face, don't tell me anything worth it) .
If you write some very simple questions, I don't think there is a few people. Here is the type in Java in Kan Yugi, this topic seems to be found, but I think you will also gain something.
To say type conversion, you must first say that Java's data type. The data type in Java is divided into two: basic types, reference types. Basic data types There are not good to say, Byte Char Short Int long float double bolean, these types except Boolean, other than the type in the C language is not much different. Because the topic of this article is type conversion, the use of boolean values will not be discussed here.
The following is the reference type. Quoted in some books is also called handle, it is similar to pointers in C / C , but pay attention to reference and pointers are not the same concept. The pointer is a variable that stores the address. He enables C / C programmers to flexibly accessing memory, but this also brings a lot of hidden dangers to the security of the program, because the programmer can operate the arithmetic operation of the pointer, so If you don't pay attention, you will break other storage units, causing unexpected results in the program. The reference inherits the advantages of the pointer to save memory, which limits the operation of the address, so he is safe. The reference type includes all types of instances and arrays (whether an object array is also the basic type array implements a Cloneable interface, so he is an object instance), all reference types are inherited from Object. To explain that all variables in Java are a reference, whether the reference type is also a basic type.
It is now necessary to formally discuss the conversion of types. People who have used C / C will be clear about basic types of conversions, and basic type conversion is divided into type lifting and forced conversion. E.g:
INT A = 100;
Long b = a 100; // This place uses the type improvement, A 100 is raised from int to Long
A = (int) b; // This place used forced conversion
Mandatory type conversion will lose the accuracy in some case, such as:
BYTE B;
INT A = 200;
B = (byte) a; // Although it is used here to use forced conversion, because Byte's range is -127 to 127
// So forced conversion, the width will be shorted
In addition to these conversions in Java, the basic data type can also be implicitly converted into string, for example:
System.out.print ("Convert" 100); // If there is a string in front of the data connection
// will be implicitly converted into String
The transition of the reference type is achieved more simple to C . If an object does not have any inheritance relationship with another object, then they cannot perform type conversion. If you want to assign a derived class object to the base class object, this is called the top. If you want to assign the base class object to the derived class object, you need to force type conversion. This is called a discontinuation, and there are some dangers of discontinuation. It is necessary to track the stake-resistant shape. The base class object must be from derived. The class object is traced.
E.g:
Class Base {}
Class Child Extends Base {
Public static void main (String [] args) {
Base base = new child (); // tracered CHILD CHILD = (child) Base; // Trace
Child Child1 = (child) new base (); // throws ClassCastException exception
}
}
Finally, talk about the conversion of string and the reference type. As mentioned earlier, all objects are inherited from Object, and there is a toString method in Object. This method is that all objects can be converted to string, and if you want to convert custom classes to String, the safest way is to override the TSTRING method. As with the basic type, if the object has a String object with a connection, the object is implicit to String, which is actually implicitly invoking the Tostring method.
Here, all topics about type conversion are finished, this is my first article, I hope everyone likes it.