When does the toUpperCase() method create a new object?












12















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?










share|improve this question




















  • 2





    I wouldn't rely on this behaviour but I would expect it to avoid creating a new object.

    – Peter Lawrey
    Nov 19 '18 at 8:00











  • Note: new String(...) doesn't change the answer.

    – Peter Lawrey
    Nov 19 '18 at 8:01






  • 8





    String x = new String("ABC"); Please don't do this. You create String object twice. just use x = "ABC";

    – Sarief
    Nov 19 '18 at 8:02











  • edit: someone pointed that OP used new String("ABC") to point to fact that it's not interned. I don't see how interning or not interning makes difference for toUpperCase(Locale) method

    – Sarief
    Nov 19 '18 at 8:21
















12















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?










share|improve this question




















  • 2





    I wouldn't rely on this behaviour but I would expect it to avoid creating a new object.

    – Peter Lawrey
    Nov 19 '18 at 8:00











  • Note: new String(...) doesn't change the answer.

    – Peter Lawrey
    Nov 19 '18 at 8:01






  • 8





    String x = new String("ABC"); Please don't do this. You create String object twice. just use x = "ABC";

    – Sarief
    Nov 19 '18 at 8:02











  • edit: someone pointed that OP used new String("ABC") to point to fact that it's not interned. I don't see how interning or not interning makes difference for toUpperCase(Locale) method

    – Sarief
    Nov 19 '18 at 8:21














12












12








12


1






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?










share|improve this question
















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?







java string object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 9:05









JJJ

29.1k147591




29.1k147591










asked Nov 19 '18 at 7:38









Rahul DevRahul Dev

16311




16311








  • 2





    I wouldn't rely on this behaviour but I would expect it to avoid creating a new object.

    – Peter Lawrey
    Nov 19 '18 at 8:00











  • Note: new String(...) doesn't change the answer.

    – Peter Lawrey
    Nov 19 '18 at 8:01






  • 8





    String x = new String("ABC"); Please don't do this. You create String object twice. just use x = "ABC";

    – Sarief
    Nov 19 '18 at 8:02











  • edit: someone pointed that OP used new String("ABC") to point to fact that it's not interned. I don't see how interning or not interning makes difference for toUpperCase(Locale) method

    – Sarief
    Nov 19 '18 at 8:21














  • 2





    I wouldn't rely on this behaviour but I would expect it to avoid creating a new object.

    – Peter Lawrey
    Nov 19 '18 at 8:00











  • Note: new String(...) doesn't change the answer.

    – Peter Lawrey
    Nov 19 '18 at 8:01






  • 8





    String x = new String("ABC"); Please don't do this. You create String object twice. just use x = "ABC";

    – Sarief
    Nov 19 '18 at 8:02











  • edit: someone pointed that OP used new String("ABC") to point to fact that it's not interned. I don't see how interning or not interning makes difference for toUpperCase(Locale) method

    – Sarief
    Nov 19 '18 at 8:21








2




2





I wouldn't rely on this behaviour but I would expect it to avoid creating a new object.

– Peter Lawrey
Nov 19 '18 at 8:00





I wouldn't rely on this behaviour but I would expect it to avoid creating a new object.

– Peter Lawrey
Nov 19 '18 at 8:00













Note: new String(...) doesn't change the answer.

– Peter Lawrey
Nov 19 '18 at 8:01





Note: new String(...) doesn't change the answer.

– Peter Lawrey
Nov 19 '18 at 8:01




8




8





String x = new String("ABC"); Please don't do this. You create String object twice. just use x = "ABC";

– Sarief
Nov 19 '18 at 8:02





String x = new String("ABC"); Please don't do this. You create String object twice. just use x = "ABC";

– Sarief
Nov 19 '18 at 8:02













edit: someone pointed that OP used new String("ABC") to point to fact that it's not interned. I don't see how interning or not interning makes difference for toUpperCase(Locale) method

– Sarief
Nov 19 '18 at 8:21





edit: someone pointed that OP used new String("ABC") to point to fact that it's not interned. I don't see how interning or not interning makes difference for toUpperCase(Locale) method

– Sarief
Nov 19 '18 at 8:21












1 Answer
1






active

oldest

votes


















18














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
}
....
}





share|improve this answer





















  • 4





    @Sarief if it created a new object (and returned that new object) x == y would definitely return false.

    – Eran
    Nov 19 '18 at 8:06






  • 2





    @Sarief it somehow got to this list, which tends to result in high traffic.

    – Eran
    Nov 19 '18 at 8:08






  • 1





    @Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

    – Sarief
    Nov 19 '18 at 8:09






  • 1





    @Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

    – nits.kk
    Nov 19 '18 at 8:12








  • 2





    @nits.kk this should not apply for immutable objects, which Strings are

    – Sarief
    Nov 19 '18 at 8:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370200%2fwhen-does-the-touppercase-method-create-a-new-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









18














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
}
....
}





share|improve this answer





















  • 4





    @Sarief if it created a new object (and returned that new object) x == y would definitely return false.

    – Eran
    Nov 19 '18 at 8:06






  • 2





    @Sarief it somehow got to this list, which tends to result in high traffic.

    – Eran
    Nov 19 '18 at 8:08






  • 1





    @Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

    – Sarief
    Nov 19 '18 at 8:09






  • 1





    @Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

    – nits.kk
    Nov 19 '18 at 8:12








  • 2





    @nits.kk this should not apply for immutable objects, which Strings are

    – Sarief
    Nov 19 '18 at 8:23
















18














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
}
....
}





share|improve this answer





















  • 4





    @Sarief if it created a new object (and returned that new object) x == y would definitely return false.

    – Eran
    Nov 19 '18 at 8:06






  • 2





    @Sarief it somehow got to this list, which tends to result in high traffic.

    – Eran
    Nov 19 '18 at 8:08






  • 1





    @Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

    – Sarief
    Nov 19 '18 at 8:09






  • 1





    @Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

    – nits.kk
    Nov 19 '18 at 8:12








  • 2





    @nits.kk this should not apply for immutable objects, which Strings are

    – Sarief
    Nov 19 '18 at 8:23














18












18








18







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
}
....
}





share|improve this answer















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
}
....
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 7:43

























answered Nov 19 '18 at 7:40









EranEran

284k37463551




284k37463551








  • 4





    @Sarief if it created a new object (and returned that new object) x == y would definitely return false.

    – Eran
    Nov 19 '18 at 8:06






  • 2





    @Sarief it somehow got to this list, which tends to result in high traffic.

    – Eran
    Nov 19 '18 at 8:08






  • 1





    @Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

    – Sarief
    Nov 19 '18 at 8:09






  • 1





    @Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

    – nits.kk
    Nov 19 '18 at 8:12








  • 2





    @nits.kk this should not apply for immutable objects, which Strings are

    – Sarief
    Nov 19 '18 at 8:23














  • 4





    @Sarief if it created a new object (and returned that new object) x == y would definitely return false.

    – Eran
    Nov 19 '18 at 8:06






  • 2





    @Sarief it somehow got to this list, which tends to result in high traffic.

    – Eran
    Nov 19 '18 at 8:08






  • 1





    @Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

    – Sarief
    Nov 19 '18 at 8:09






  • 1





    @Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

    – nits.kk
    Nov 19 '18 at 8:12








  • 2





    @nits.kk this should not apply for immutable objects, which Strings are

    – Sarief
    Nov 19 '18 at 8:23








4




4





@Sarief if it created a new object (and returned that new object) x == y would definitely return false.

– Eran
Nov 19 '18 at 8:06





@Sarief if it created a new object (and returned that new object) x == y would definitely return false.

– Eran
Nov 19 '18 at 8:06




2




2





@Sarief it somehow got to this list, which tends to result in high traffic.

– Eran
Nov 19 '18 at 8:08





@Sarief it somehow got to this list, which tends to result in high traffic.

– Eran
Nov 19 '18 at 8:08




1




1





@Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

– Sarief
Nov 19 '18 at 8:09





@Eran Let me rephrase, even if it had in contract that it creates a new object and it did create a new object, it does not mean that it will return a new object. Strings are contained in Strings pool and are reused mostly from there

– Sarief
Nov 19 '18 at 8:09




1




1





@Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

– nits.kk
Nov 19 '18 at 8:12







@Sarief First of all its not silly question, as Strings are immutable so any operation performed is expected to result in a separate object. Secondly the answer as many would think is related to same string in string pool, but its not as OP has created string using new and not invoked intern() hence correctly the answer points the implementation bwing the result as why no new object was created for the case presented by the question

– nits.kk
Nov 19 '18 at 8:12






2




2





@nits.kk this should not apply for immutable objects, which Strings are

– Sarief
Nov 19 '18 at 8:23





@nits.kk this should not apply for immutable objects, which Strings are

– Sarief
Nov 19 '18 at 8:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370200%2fwhen-does-the-touppercase-method-create-a-new-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()