-1

public class MyClass {

public static void main(String args[])  {
String s="this is java";
String name[]=s.split(" ");
boolean k;
System.out.println(name[1]);
k=(name[1]=="is");
System.out.println(k);

}}

in this code i am getting output: is false

why k is returning false ?

1 Answers1

-1

You have to understand how == compares equality.

For primitive types, == will compare by value. For objects, == will compare by reference. So object1 == object2 will return true only when both object1 and object2 has the same reference. This is the reason why it returns false because the references are different.

If you change the statement to name[1].equals("is") it will return true.