Deep understanding: Java is a type-safe language, while C + + is a _c-safe language

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.