Why does the else statement gets executed here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does the else statement gets executed here?

<TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="your"/> text3 = (TextView) findViewById(R.id.text3); String c = text3.getText().toString(); String b = "your"; if(c == b) { Toast.makeText(getApplicationContext(), "SUCCESS ", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getApplicationContext(), "Fail ", Toast.LENGTH_SHORT).show(); }

26th May 2019, 12:36 PM
Ghost rider
3 Answers
+ 19
▪equals() method == tests for reference equality, (whether they are the same object) .equals( ) tests for value equality, (whether they are logically "equal") ➝ equals() is a method used to compare two objects for equality. The default implementation of the equals() method in the Object class returns true if and only if both references are pointing to the same instance. It therefore behaves the same as comparison by == .
26th May 2019, 1:17 PM
Danijel Ivanović
Danijel Ivanović - avatar
0
String a,b; a = "AA"; a = a +"A"; //AAA b = "AAA"; System.out.println( a == b ); //false System.out.println( a.equals(b) ); //true
26th May 2019, 5:01 PM
zemiak