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

Source: Internet
Author: User

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.

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.