Some example of a 'faster' one - I picked special Strings to test, because java contains
method already checks the length, which makes this approach and your approach basically the same if Strings differ in length (one contains
is cut basically immediately after checking that first argument has shorter length). Also, since contains
shortcuts after it finds that it cannot be true anymore, I made them differ only at the end. They are both really long Strings, you won't possibly encounter such Strings in your real world.
This approach is 2 times faster, because your is making a full check twice and this only once, but the difference is negligible, in a real benchmark it would be even smaller. And the Strings are cherry picked for the difference to be the biggest possible, so...
In conclusion: it's fine as it is if you want, but actually to me this version looks cleaner.
public static void main(String[] args) {
String a = "";
String b = "";
Random random = new Random();
StringBuilder builder = new StringBuilder(119999999);
for(long i = 0; i < 119999999; i++)
builder.append((char) random.nextInt());
a = builder.toString() + "zzzzzzzzz";
b = a.substring(0, a.length() - 9) + "dsadsasda";
Instant start = Instant.now();
boolean test = a.length() > b.length() ? a.contains(b) : b.contains(a);
//boolean test = a.contains(b) || b.contains(a);
if(test)
System.out.println("OK");
System.out.println(Duration.between(start, Instant.now()).toMillis());
}