0
String a ="abc";
return (a.substring(1)=="bc");

I tried to print the result of
a.substring(1) which is also
"bc"

Why the result is false? I think it's true.

Cindy
  • 119
  • 1
  • 1
  • 7
  • substring returns new instance of String representing `"bc"` which is different that `"bc"` literal interned in string pool so `==` (reference comparition) returns false – Pshemo Oct 22 '14 at 18:45

2 Answers2

1

== compares references and the value of primitives (int, long etc), use a.substring(1).equals("bc") instead.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
1

It should be like this:

String s = "abc";
System.out.println(s.substring(1).equals("bc"));
Jake Huang
  • 140
  • 1
  • 12