Correct understanding of Java method's return value

First look at a piece of code

Correct understanding of Java method's return value
We put this code called the code ①, then we will look at another piece of code

Correct understanding of Java method's return value
We put this code called the code ②.
① in the code which defines a method, which declared return type is double, while the actual returned by the return keyword is a value of type int, but did not cause compile errors. In the code ② them, the opposite is true, the method declaration return type is int, the return keyword inside the actual method returns a value of type double, in this case, the compiler has reported a syntax error.
Compare these two cases, many novice Java junior partner confused: Why is the method's return value type of an actual return values are not the same, but the case is not being given, and one in which they can not pass compile check it? Talk clearly this question, we must from Why To declare the return type start.
In the Java language, requires the programmer to define a method, you must declare the return type of this method in front of the method name. Why ask you to do it? Simply put, the compiler forces the programmer must open a message, it is this: After you run this method returns a value of any type, the only way people know to use a method call what type of variable receiving method of operation result. In addition, the compiler further requirements: the scheduling method in the process of receiving a return value, the return value must be declared as a standard type, instead of watching the actual type of the return value. Take the code ①, the return value type is a double declaration, the actual return value is int, we are receiving method returns a value of time must be received by double-type variables using variable procedures, even if it's just the actual returns an int data, nor can an int variable that receives the return value, otherwise the compilation error occurs

Next, let us observe the process of defining the method. In the code ① among the methods declared return type is double type, but actually inside the method returns a value of type int, between the two types are not consistent, but there is no syntax error, this is how it happened? This is because, Java language syntax-checked at the time, requires the declaration of the return type and the type of the actual return value of long reach "compatible" on it, you do not need to be identical. ① method code to do both of which are compatible, but the method code among ② did not, so the error.
For example easy to understand: when you were young, your father to go out, to tell you when he will be back to bring back a 50 cm diameter cake, then you will certainly be in accordance with his promise, ready with a diameter of 50 cm platter used to hold this big cake. But your father came back, but it is actually brought back a diameter of only 10 cm small cake, then although you are very sad and very disappointed, but you prepared platter enough to be able to let go of your father brought back small cakes, not out of any problem. Method ① which codes defined This is the case, a return statement 8 bytes of double-data, but the actual account returns a 4-byte int type.
But in turn, your dad tells you to go out when he came back will bring back a small 10cm cake, you also prepared a diameter of 10 cm small dish according to his promise, intends to install this little cake. But when your father came back, but it is brought back up to a diameter of 50 cm cake! At this point, you surprise and excitement, but it can not solve a problem: you simply can not prepare a small dish full bloom so much cake! It is like the case of the code ② in the show, the statement returns an int data type only a 4-byte, and the actual operation method returns an 8-byte double-data, so that those who are ready to receive a variable of type int method returns a value the man was "taken by surprise." Of course, this is just a joke, if there was a real case of the return value is not compatible, the compiler did not allow such a code through the compiler, so it is much less run the code.
Next, under what circumstances and then talk about the specific method return value type declaration and the actual return value type can achieve compatibility. We can discuss the following situations:
First, we know that, Java basic data types 8 species, of which there are four in integer types, namely, long, int, short and byte. The four integer types have a clear " backward compatible " feature, that is to say, the return value is declared larger data types, actual return to smaller data types, is certainly no problem. For example, the statement returns a long type, but the actual return int type data is no problem. Again, both floating-point double and float can also be "backward compatible."
Second, we also know, represents char type characters in the actual process of storing data, storage is also a two bytes of "integer", the "integer" is actually encoded value of the character. So char type of data can be compatible with Java int with four kinds of it? The reality is: Long and int fully compatible with char, and short and byte not. In turn, char type is not compatible with any kind of integer data . He said bluntly that if we put the return value declared as char type method, the method actually returns a long, int, short and byte data type will not work.
Third, integer and floating-point numbers can be compatible with any character. In other words, Double and float compatible with long, int, short, byte and char. But then, integer and character can not be compatible with any of the float . There may be a little puzzled write small partners: 4-byte float really compatible 8-byte long it? From the perspective of grammar is not a problem, we have a long type of data assigned to a variable of type float do not need to cast, the compiler will not complain. But the actual stored procedure, may cause loss of data accuracy. Note here that "possible" loss of accuracy, rather than "must" loss of accuracy.
Four, boolean type is "Maverick" in the eight kinds of basic data types, it is not compatible with any other type of data baseIf a method declaration return type boolean, then this method can only return boolean types of data.

The above method we talked about the return value is the underlying data type, if the reference data types, the situation then? In this case, follow the " father of compatible child principle". That is, if the return value is declared as a parent class, but actually returns an object subclass objects can pass compiler syntax checking. Similarly, if the return type is declared as an interface, the method returns the object to the actual implementation class interface object, there is no problem. In turn, the return type of the method declaration subclass, the actual return value is the parent object of inspection will not compile.

I hope this method returns a little understanding of the value of Java beginner partners help.

If you want to learn Java programming system, and welcome to my video lessons on this site.

Guess you like

Origin blog.51cto.com/2266836/2480984