Java is a type-safe language, and C + + is non-type safe? Explain

Source: Internet
Author: User

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

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.