-1

Can someone explain me what is the difference between these two line of code? First returns true as result whereas second line of code always return false. I have confirmed driver.getTitle() in my case returns "Google".

if (driver.getTitle().equals("Google")) //works and return true

if (driver.getTitle() == "Google") //doesn't work and always return false.

Here is full code for reference:

package org.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

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

WebDriver driver;
driver = new FirefoxDriver();
driver.get("http://www.google.com");

String title = driver.getTitle();

// Assert.assertEquals(title, "GoogleFAIL", "Test case has been FAILED");
//if (driver.getTitle().equals("Google"))

if (driver.getTitle() == "Google"){
    System.out.println("Test case has been passed.");
} else
{
    System.out.println("Test case has been failed.");
}
driver.close();
}
}
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Asif Nisar
  • 15
  • 7

1 Answers1

3

Here basically both are used to compare strings but the major difference is equals() is used to compare values and state of two objects, where as == is called as reference equality, this means == is used to check the memory location of two objects on the heap are same or not, but not the value.

here is more info http://java67.blogspot.in/2012/11/difference-between-operator-and-equals-method-in.html

user3872094
  • 3,269
  • 8
  • 33
  • 71