People with C ++ development experience will find that we can use 0 as false and non-zero as true. A function is of the bool type, but we can still return the int type, and automatically convert 0 to false, non-zero to true. The code example is as follows:
Copy codeThe Code is as follows: # include <iostream>
# Include <stdlib. h>
Using namespace std;
Bool fun () // The return type of the function is bool, but we can return the int type in the function.
{
Return 1;
}
Void main ()
{
Int a = 1;
If (a) // a is of the int type, but can be used as the bool type.
{
Cout <"C ++ is non-type secure. "<Endl;
}
System ("pause ");
}
However, in java, we cannot use this method. In java, we cannot convert the int type to the bool type. For example, the following code:Copy codeThe Code is as follows: public class TypeSafeTest {
Public static void main (String [] args ){
Int I = 1;
If (I)
{
System. out. println ("java is a type security language ");
}
}
}
The following error is reported when you execute the above Code:Copy codeThe Code is as follows: Exception in thread "main" java. lang. Error: Unresolved compilation problem: Type mismatch: cannot convert from int to boolean
At TypeSafeTest. main (TypeSafeTest. java: 4)
The preceding error indicates that the int type cannot be automatically converted to the bool type in java. This is the meaning of type security.