People with C + + development experience will find that we can use 0 as false, not 0 as true. A function is of type bool, but we can return the type of int and automatically convert 0 to False, not 0 to true. The code example is as follows:
Copy Code code as follows:
#include <iostream>
#include <stdlib.h>
using namespace Std;
the bool Fun ()//function return type is bool, but we can return the int type in the function.
{
return 1;
}
void Main ()
{
int a=1;
The IF (a)//a is of type int, but can be used as a bool type.
{
cout<< "C + + is non type safe. "<<endl;
}
System ("pause");
}
However, in Java, we can not use this, Java can not do the type of int type bool, such as the following code:
Copy Code code as follows:
public class Typesafetest {
public static void Main (string[] args) {
int i=1;
if (i)
{
System.out.println ("Java is a type-safe language");
}
}
}
Executing the above code will report the following error:
Copy Code code as follows:
Exception in thread ' main ' java.lang.Error:Unresolved compilation Problem:type mismatch:cannot convert from int to bool Ean
At Typesafetest.main (typesafetest.java:4)
The above error indicates that the int type cannot be converted to bool type in Java. This is the meaning of type safety.