Why doesn't both these codes give the same output?

Question

So I want draw a field (of array strings) with obstacles and free spaces. "+" are the obstacles and free spaces are just "". And if we have a free space, I want it replaced by "*". I will choose one specific element of that 2d array that equals "" and that will be replaced by "1".

My question isn't as long as it seems, you could ignore the 2 codes below. Here is summary, if unclear, please read to the end:

Why aren't these things equal to each other?

if(field[i][j]==field[5][1]){
  System.out.print("1");
}

and

if(i==5 && j==1){
   System.out.print("1");
}

First code doesn't work. That specific array element here will be field[5][1] which I want replace by 1:

public class Test{
    public static void main(String[] args){

        String[][] field = {{"+" , "+" , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "" , "+"},
                            {"+" , "+" , "+" , "+" ,""  , ""  , "+"},
                            {"+" , "+" , "+" , ""  ,""  , "+" , "+"},
                            {"+" , ""  , ""  , ""  ,"+" , "+" , "+"},
                            {"+" , ""  , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "+" , "+"}};
        int x = field.length;

        for(int i=0; i<x; i++){
            for(int j=0; j<field[i].length; j++){
                System.out.print(field[i][j]);

                if(field[i][j] != "+"){
                    if(field[i][j]==field[5][1]){
                        System.out.print("1");
                    }
                    else{
                        System.out.print("*");
                    }
                }
            }
            System.out.println("");
        }
    }
}

Output:
+++++++
+++++1+
++++11+
+++11++
+111+++
+1+++++
+++++++

Second code works as desired:

public class Test{
    public static void main(String[] args){

        String[][] field = {{"+" , "+" , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "" , "+"},
                            {"+" , "+" , "+" , "+" ,""  , ""  , "+"},
                            {"+" , "+" , "+" , ""  ,""  , "+" , "+"},
                            {"+" , ""  , ""  , ""  ,"+" , "+" , "+"},
                            {"+" , ""  , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "+" , "+"}};
        int x = field.length;

        for(int i=0; i<x; i++){
            for(int j=0; j<field[i].length; j++){
                System.out.print(field[i][j]);

                if(field[i][j] != "+"){
                    if(i==5 && j==1){
                        System.out.print("1");
                    }
                    else{
                        System.out.print("*");
                    }
                }
            }
            System.out.println("");
        }
    }
}

Output: 
+++++++
+++++*+
++++**+
+++**++
+***+++
+1+++++
+++++++

Show source
| java   | arrays   | string   | syntax   | printing   2017-01-05 11:01 4 Answers

Answers to Why doesn&#39;t both these codes give the same output? ( 4 )

  1. 2017-01-05 11:01
    if(field[i][j]==field[5][1]){
      System.out.print("1");
    }
    

    This this compares value of field[5][1] which is equals to "" and every field that has value "" prints number "1". This code doesn't check the field position but compares values.

  2. 2017-01-05 11:01

    The two are obviously different:

    if(field[i][j]==field[5][1]){
    

    Is true if field[i][j] is "" - and there are lots of array elements equal to that.

    if(i==5 && j==1){
    

    is true if i == 5 and j == 1 - which is only true at 1 position.


    It should be noted that the first approach is not a reliable way to compare strings: it is only because of the way that you construct field that the code currently behaves as it does. In general, you should use field[i][j].equals(field[5][1]). See How do I compare strings in Java.

  3. 2017-01-05 11:01
    if (field[i][j] == field[5][1]) {
      System.out.print("1");
    }
    

    checks whether the current field's value is equal to the value of field[5][1] (that's "" in your case). As you can see, the first output marks every "" from field with a 1.

    if (i==5 && j==1) {
       System.out.print("1");
    }
    

    checks whether the current position is 5,1. Values of field are not considered.

  4. 2017-01-05 11:01

    if(i==5 && j==1) is true for only one case, if(field[i][j]==field[5][1]) is true for all the case with the same value than field[5][1]. ("")

    If your question is "why == work between two different string?"

    The cause is an optimisation of the compilator, who will create a constante for each identical string , so when == compare the reference of two string with different (i,j), it's in fact the same String Object, so it's true.

Leave a reply to - Why doesn't both these codes give the same output?

◀ Go back