Ternary operator to return value- Java/Android

后端 未结 5 696
情深已故
情深已故 2021-02-07 01:13

Just switched to Java from php

I encountered following issue

I want to rewrite

if(usrname.equals(username) && (passwd.equals(password))){         


        
5条回答
  •  有刺的猬
    2021-02-07 01:56

    By the way you can simplify:

    return (usrname.equals(username) && passwd.equals(password) )? return true : return false;

    To:

    return usrname.equals(username) && passwd.equals(password);

    The ternary operator work similar in php than Java, I think you have made a silly mistake, maybe "username" have a space or another white character

    String a1 = "a";
    String b1 = "b";
    
    String a2 = "a";
    String b2 = "b";
    
    System.out.println((a1.equals(a2)&&b1.equals(b2))?"true":"false");
    

    it return "true"

提交回复
热议问题