0

I don't understand why the following if statement never hits, I've verified through the log that movie and films.get(i).getTitle() are the same string:

List<Film> films = new ArrayList<Film>();
films = filmDB.getAllFilms();



for(int i = 0; i < films.size(); i++)

                if(movie == films.get(i).getTitle())
                {
                    Log.d("TITLEMOVIE", films.get(i).getTitle());
                    ;
                }
            else
                {
                    Log.d("NOMATIC", films.get(i).getTitle());
                }
Se O
  • 3
  • 1

1 Answers1

0

== for anything but primitives compares the memory addresses of the two objects. use .equals to compare strings and other objects.

i.e.;

String str1 = "bla";
// Use String::new to make sure 'str1' and 'str2' don't reference the same literal
String str2 = new String("bla");

System.out.println(str1 == str1); // true
System.out.println(str1 == str2); // false
System.out.println(str1.equals(str2)); // true
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93