have ever C + + people who develop experience will find that we can 0 as a false , not 0 as true . A function, even if it is of type bool, but we can return the int type, and automatically convert 0 to false , non-0 converts into true . The code example is as follows:
#include <iostream> #include <stdlib.h>using namespace Std;bool fun ()//The function return type is bool, but we can return the int type in the function. { return 1;} void Main () { int a=1; 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't use it that way, Java can't do it. int type Go BOOL type, such as the following code:
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:
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 theJavainintthe type cannot automatically turn intoBOOLtype. This is
type safetyThe meaning.
Java is a type-safe language, and C + + is non-type safe? Explain