-3
public static void main(String[] args) {
        String a = new String("abc");
        String b = "abc";
        String c = "abc" + "KDF" + "gh" +"rt" +"jk";

    }

Please find below byte code of the code.

Compiled from "NumOfStringObject.java"
public class com.practise.general.NumOfStringObject extends java.lang.Object{
public com.practise.general.NumOfStringObject();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   new     #2; //class java/lang/String
   3:   dup
   4:   ldc     #3; //String abc
   6:   invokespecial   #4; //Method java/lang/String."<init>":(Ljava/lang/Strin
g;)V
   9:   astore_1
   10:  ldc     #3; //String abc
   12:  astore_2
   13:  ldc     #5; //String abcKDFghrtjk
   15:  astore_3
   16:  return

}

I have no experience in reading/understanding byte code, i guess total of 3 String objects are getting created.

let me know your thoughts.

Lovesh
  • 3
  • 1

3 Answers3

4

Yes, three. At first glance, you'd think more, but the compiler will combine string constants; details in JLS§15.28 - Constant Expressions. So the compiler effectively treats the body of your main as:

String a = new String("abc");
String b = "abc";
String c = "abcKDFghrtjk";

It also reuses the same string for equivalent constants in the class (which are interned, so the same string can be used across classes as well), which means there's only one "abc" created by the two occurrences of the "abc" literal in that code.

So:

  • One "abc" from the constant (literal)
  • One "abcKDFghrtjk" from the other constant (result of combining several literals)
  • One "abc" you created explicitly as a separate object in the first line of code via new String
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hi Sir TJ, Im wondering if only `String literals` are only treated constants in compilation? – msagala25 Dec 07 '16 at 08:33
  • 1
    @msagala: Anything that's a constant value per the specification; [details here](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28). So that's literals, and also references to [`final` string variables and fields](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4) (which the spec sometimes calls member variables). – T.J. Crowder Dec 07 '16 at 08:37
2
  1. literal "abc" is created.
  2. String having same characters as "abc" is created.
  3. literal "abcKDFghrtjk" is created.

Specification:

"This is a string"    // a string containing 16 characters
"This is a " +        // actually a string-valued constant expression,
    "two-line string"    // formed from two string literals

Strings computed by concatenation at run time are newly created and therefore distinct.
Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

xenteros
  • 15,586
  • 12
  • 56
  • 91
-4

String is immutable

Read the discussion here. Should help you. String is immutable. What exactly is the meaning?

In brief. If you ever witness your String variable changing its value in your code, you can safely assume that a new object is created.

String s = new String("some value"); 

and

String s = "some value"; 

are very same. When you use = the value is not set but a new object is actually created under the same reference s

EDIT: For the "Downvoters", the answer is YES. Three objects are created

Community
  • 1
  • 1
madhairsilence
  • 3,787
  • 2
  • 35
  • 76
  • 5
    How does this relate to the question of how many strings are created in the given code? Besides, `"some value"` was already created in the string pool in the first statement, and `s` is merely assigned an existing reference. Nothing to do with immutability. – RealSkeptic Dec 07 '16 at 08:24
  • 1
    The number of strings created is not related to the fact that they are immutable. And the statement "When you use `=` the value is not set but a new object is actually created" is wrong when the value is a literal that has already been used in the program. – RealSkeptic Dec 07 '16 at 08:29
  • @BrandonIbbotson thats why I put a line "Whenever u see the string changing value. a new object is created". Not sure why is this answer went completely misleading – madhairsilence Dec 07 '16 at 08:29