Possible error in ocp-815 enthuware.ocpjp.i.v11.2.3052
Posted: Mon Jan 13, 2020 10:26 pm
Hi guys)
Here is the screen of quesion
The question is about third option `str.strip()!=""`.
From the explanation, we can see that if string consist of only whitespaces, strip will return empty string, but it maybe a different object.
But if we go to `strip` method we see
If we go deeper we will see
and
From above it's clear that both of them return empty string(if it's only whitespaces) as literal, not as new object, so that proves that third option also correct.
Pay attention that it's not true for `trim` method.
Here is the screen of quesion
The question is about third option `str.strip()!=""`.
From the explanation, we can see that if string consist of only whitespaces, strip will return empty string, but it maybe a different object.
But if we go to `strip` method we see
Code: Select all
public String strip() {
String ret = this.isLatin1() ? StringLatin1.strip(this.value) : StringUTF16.strip(this.value);
return ret == null ? this : ret;
}
If we go deeper we will see
Code: Select all
public static String strip(byte[] value) {
int left = indexOfNonWhitespace(value);
if (left == value.length) {
return "";
} else {
int right = lastIndexOfNonWhitespace(value);
return left <= 0 && right >= value.length ? null : newString(value, left, right - left);
}
}
Code: Select all
public static String strip(byte[] value) {
int length = value.length >> 1;
int left = indexOfNonWhitespace(value);
if (left == length) {
return "";
} else {
int right = lastIndexOfNonWhitespace(value);
return left <= 0 && right >= length ? null : newString(value, left, right - left);
}
}
From above it's clear that both of them return empty string(if it's only whitespaces) as literal, not as new object, so that proves that third option also correct.
Pay attention that it's not true for `trim` method.
Code: Select all
public class App {
public static void main(String[] args){
System.out.println(isBlankStrip(" ")); // true
System.out.println(isBlankTrim(" ")); // false
}
private static boolean isBlankStrip(String str){
String empty = str.strip();
return empty == "";
}
private static boolean isBlankTrim(String str){
String empty = str.trim();
return empty == "";
}
}