0

I need help with figuring out how many objects are created by the code I will provide.

I would assume that there is only one object because there is no use of the "new" operator, but I am not sure.

System.out.print("Enter a sentence : ");

mySentence = keyboard.next();

System.out.println("The original is  : " + mySentence);

mySentence.toUpperCase();

System.out.println("The same one is  : " + mySentence);

mySentence = mySentence.toUpperCase();

System.out.println("The raised is    : " + mySentence); 
bigshirtjp
  • 43
  • 6
  • At least three. One on `keyboard.next()`, a second on the first toUpperCase (with no reference saved), and then the third on the second `toUpperCase()`. It's possible that the JIT will eliminate that first toUpperCase. – Elliott Frisch Apr 30 '19 at 19:28
  • @ElliottFrisch: Right, I didn't count objects consumed immediately. – Nikolas Charalambidis Apr 30 '19 at 19:30
  • @ElliottFrisch 4-6 not counting literals. One for string returned by `keyboard.next()`, 3 for the string concatenations. Then maybe 2 more if `mySentence` isn't already all uppercase. – Andreas Apr 30 '19 at 20:46
  • @MrSpark Why do you believe anything other than the string literals are stored in string pool? There are no [`intern()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#intern--) calls anywhere. – Andreas Apr 30 '19 at 20:49

3 Answers3

0

Each time you call toUpperCase() a new String object is created. Even though it may seem like it is the same object, they're technically different objects in memory, as shown by this code

String x = "hello";
String y = x.toUpperCase();
System.out.println(x.equals(y));
//yields false

So to answer your question, I would say that there are 3 objects created; although you are not saving the second one anywhere, it still is created and then immediately thrown away

Grimmpier
  • 74
  • 1
  • 9
  • Just realized that String.equals() does not compare memory addresses, but there are still 3 objects being created in that code snippet – Grimmpier Apr 30 '19 at 19:40
  • 2
    Actually, if we don't count the literal strings, there are 6. Each concatenation creates a new string. – JB Nizet Apr 30 '19 at 19:44
  • Actually, to `toUpperCase()` doesn't create a new string if it is already uppercase, so: **4-6**. – Andreas Apr 30 '19 at 20:55
0

Tldr: it depends.

Each time you create a String, you create a new object as well (but this also depends on the String pool). You can prove this by comparing two strings with ==. == tests for reference equality (whether they are the same object), while .equals() tests for content equality.

System.out.print("Enter a sentence : ");
final String mySentence = keyboard.next();
System.out.println("The original is  : " + mySentence);
final String mySentence2 = mySentence.toUpperCase();
System.out.println("The same one is  : " + mySentence);
final String mySentence3 = mySentence.toUpperCase();
System.out.println("The raised is    : " + mySentence3); 
System.out.println(mySentence == mySentence2);
System.out.println(mySentence3 == mySentence2);
System.out.println(mySentence3 == mySentence);

The output for input string "FOO" is (1 object created)

The original is  : FOO
The same one is  : FOO
The raised is    : FOO
true
true
true

The output for input string "foo" is (3 objects created)

The original is  : foo
The same one is  : foo
The raised is    : FOO
false
false
false

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 (see here).

(plus 7 objects if you count the Strings in println)

w4bo
  • 855
  • 7
  • 14
0

I count ten:

  1. The first statement creates a String ("Enter a sentence : ") (1)
  2. The second statement creates a String (mySentence) (2)
  3. The third statement creates 2 strings (""The original is : "" and the concatenated string) (3,4)
  4. The fourth sentence creates a new string (then discards it) (5)
  5. The fifth statements creates 2 more (6,7)
  6. the sixth statements creates one more (8)
  7. the final statement creates 2 more (9,10)
FredK
  • 4,094
  • 1
  • 9
  • 11
  • 1
    #5 and #8 may be the same as #2, if `mySentence` is already uppercase, so maybe only 8. --- For questions like this, you don't usually consider string literals to be "created", so that eliminates #1, #3, #6, and #9, so I believe the correct answer is: **4-6**. – Andreas Apr 30 '19 at 20:53