public class Child{
public static void main(String[] args){
String x = new String("ABC");
String y = x.toUpperCase();
System.out.println(x == y);
}
}
Output: true
So does toUpperCase()
always create a new object?
Description. The toUpperCase() method returns the value of the string converted to uppercase. This method does not affect the value of the string itself since JavaScript strings are immutable.
The “toUpperCase()” method is used for converting “string” characters to “uppercase” format. As JavaScript “string” is of “immutable” type, the “toUpperCase()” method returns a new string and does not modify the original string.
Javascript toUpperCase() is a built-in string function that converts a string to uppercase letters. The toUpperCase() method returns the calling string value converted to uppercase. To convert a string to uppercase in JavaScript, use the toUpperCase() method.
The java. lang. Character. toUpperCase(char ch) converts the character argument to uppercase using case mapping information from the UnicodeData file.
toUpperCase()
calls toUpperCase(Locale.getDefault())
, which creates a new String
object only if it has to. If the input String
is already in upper case, it returns the input String
.
This seems to be an implementation detail, though. I didn't find it mentioned in the Javadoc.
Here's an implementation:
public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstLower;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstLower = 0 ; firstLower < len; ) {
int c = (int)value[firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR)
|| (c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this; // <-- the original String is returned
}
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With