简体   繁体   中英

Java ternary operator behavior confusing

I cam across a confusion regarding the use of Colon operator in java. In my code I used the following line

int replace=L[i-1][j-1]+P.charAt(i-1)!=T.charAt(j-1)?1:0;

But that was not giving correct result made me confused. Then I changed the above line with the following

L[i-1][j-1]+(P.charAt(i-1)!=T.charAt(j-1)?1:0);

It worked fine. I wonder why is this happen. I have the following ideone links written using C and Java. They are working without that first bracket. But why in my code it was not giving result.

https://ideone.com/QMYaNy
https://ideone.com/QhBwkt

For better understanding I am giving my full code below:

public class EditDistance {
    static int [][] L;
    public static void main(String[] args) {
        String P="SUNDAY";
        String T="SATURDAY";
        minedit(P, T);



}


static void minedit(String P,String T)
{
    int m=P.length()+1;
    int n=T.length()+1;
    L=new int[m][n];
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            L[0][j] = j;
            L[i][0] = i;
        }
    }

    //System.out.println(L[0][0]);
    for(int i=1;i<m;i++)
    {
        for(int j=1;j<n;j++)
        {
            int insert=1+L[i][j-1];
            int delete=1+L[i-1][j];
            //int replace=L[i-1][j-1]+P.charAt(i-1)!=T.charAt(j-1)?1:0;
            int replace=L[i-1][j-1]+(P.charAt(i-1)!=T.charAt(j-1)?1:0);
            L[i][j]=minimum(insert, delete, replace);
        }
    }

     for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                System.out.print(L[i][j]+" ");
            }
            System.out.println();
        }

    System.out.println("Minimum Edit Distance is "+L[m-1][n-1]);
}

public static int minimum(int a,int b,int c)
{
    int temp=a<b?a:b;
    return temp<c?temp:c;
}

}

In your first example, replace will always just be 1 or 0, because

L[i-1][j-1]+P.charAt(i-1)!=T.charAt(j-1)

gets evaluated as the boolean expression. This part of the expression will get calculated first:

L[i-1][j-1]+P.charAt(i-1)

And then it will check if that is equal to:

T.charAt(j-1)

If it is not equal, replace will be 1. If it is equal, replace will be 0.

Wrapping

P.charAt(i-1)!=T.charAt(j-1)?1:0    

in parentheses will cause it to be evaluated first as 1 or 0, then that result will be added to

L[i-1][j-1]

u have to know that in the line:

int replace=L[i-1][j-1]+P.charAt(i-1)!=T.charAt(j-1)?1:0;

the part:

P.charAt(i-1)!=T.charAt(j-1)?1:0;

its only an inline if, that is readed this way:

if(P.charAt(i-1)!=T.charAt(j-1))
{
     return 1;
}
else
{
     return 0;
}

in other words if P.charAt(i-1)!=T.charAt(j-1) is true:

int replace=L[i-1][j-1] + 1;

if it is false:

int replace=L[i-1][j-1] + 0;

i hope this helps u.

regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM