Search code examples
javac++compilation

For what types of A and B is the simple assignment statement A = B legal in C++ but not Java?


Got this question in my homework, tried googling but no luck there either.

For what types of A and B is the simple assignment statement A = B legal in C++ but not Java?


Solution

  • Anything with a boolean assignment should do. In C++, true is any non-zero value. In Java, boolean is a type (and that type is checked). For example,

    bool a = 1;
    

    is legal C++. But

    boolean a = 1;
    

    is not legal Java. Also, as noted by @akuzmiykh the reverse is also true, that is

    int a = true;
    

    is legal C++, but not legal Java.