mercredi 18 décembre 2019

Why does the if statement "if(1)" give error in Java but it holds true in C++? [closed]

It might be trivial, but I am a beginner in Java so any help will be much appreciated! The error I get if I run the program

public class Main {
    public static void main(String args[]) {
        if(1)
           System.out.println("Hello World!");
    }
}

is

Main.java:3: error: incompatible types: int cannot be converted to boolean if(1) ^

but the following code in C++ works

#include <iostream>
using namespace std;
int main() {
    if(1)
        cout<<"Hello World!";
}

Does Java not consider 0 as false and any number other than 0 true, like in C++?

Aucun commentaire:

Enregistrer un commentaire