Search code examples
javastringif-statementequals-operator

If else statement with a string compare java


I am very new to java code, only ever really used C++ before. I am trying to compare two string variables and if they match set a new variable to string answer else new variable would be null.

e.g.

if (ID.equals(DeviceID)){
        MobileDevice = "BB 9630";
     else
        MobileDevice = null;
     end if

But this does not seem to work when I try it, and I think the logic is correct...any help?


Solution

  • A simpler way to do this is to use a tri-graph, something you can do in C++ as well.

    String mobileDevice = id.equals(deviceId) ? "BB 9630" : "unknown";
    

    You should use camelCase for variables in Java.