Reputation: 21358
Can someone please tell me how many objects will be created on executing the System.out.println
statement in the below code
int i=0;
int j=1;
System.out.print("i value is "+ i + "j value is "+j);
Upvotes: 10
Views: 5334
Reputation: 1
You should consider 2 points here:
In this case the above answer is correct. Five String objects will be created.
Upvotes: 0
Reputation: 410
5 objects
Upvotes: 0
Reputation: 43798
This will create a StringBuilder
object (and whatever this object uses internally), add the values and finally the StringBuilder
will create a String
object with the result.
Upvotes: 4
Reputation: 6610
If you really want to know what's going on, why not look at the bytecode?
I wrapped your code in a main function, compiled it and then disassembled it with javap -c Test.class
. Here's the output (using a Oracle Java 7 compiler):
Compiled from "Test.java"
class Test {
Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iconst_1
3: istore_2
4: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
7: new #3 // class java/lang/StringBuilder
10: dup
11: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
14: ldc #5 // String i value is
16: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
19: iload_1
20: invokevirtual #7 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
23: ldc #8 // String j value is
25: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
28: iload_2
29: invokevirtual #7 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
32: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
35: invokevirtual #10 // Method java/io/PrintStream.print:(Ljava/lang/String;)V
38: return
}
The only object that gets allocated in this method is the StringBuilder
(by the new
instruction at position 7). However, the other methods that are invoke
d might allocated something themselves, and I have a very strong suspicion that StringBuilder.toString will allocate a String
object.
Upvotes: 7
Reputation: 5440
The code
int i=0;
int j=1;
System.out.print("i value is "+ i + "j value is "+j);
Creates 3 objects.
My Reason:
The basic data types in Java are not objects and does not inherit from Object. so
int i=0; and int j=1;
does not make an object.
Now System.out.print("i value is "+ i + "j value is "+j);
which contains String
which are immutable, and the operations on string are costly.We can split the operations as this.
("i value is ").concat(i) // creates one object let say obj1
obj1.concat("j value is ") //creates another let say obj2
obj2.concat(j) // creates the final string let say obj3;
In an example string operation str1.concat(str2)
is done by using two String objects and it creates the third one and change the reference making an illusion that its actually the first string object ie str1. Thus the str1 will be having a new String which contains the value of the old str1 and the str2 concatenated.
This is what i believe with my limited knowledge. Correct me if i am wrong.
Upvotes: 2
Reputation: 96016
If it is indeed implementation dependent, then if it is evaluated to:
StringBuilder sb = new StringBuilder("i value is ");
sb.append(i);
sb.append(j);
String newStr = sb.toString();
There will be 2 objects.
Upvotes: 1
Reputation: 395
String is immutable means that you cannot change the object itself.
While performing concatenation every time new string objects are created.
So in above example
Total six objects are created in above example.
Upvotes: 0
Reputation: 58927
The code is converted, by the compiler, to something like this:
int i=0;
int j=1;
StringBuilder temp = new StringBuilder(); // creates a StringBuilder
temp.append("i value is "); // creates or re-uses a String
temp.append(i); // might create a String
temp.append("j value is"); // creates or re-uses a String
temp.append(j); // might create a String
String temp2 = temp.toString(); // creates a String
System.out.print(temp2);
It depends on whether you count the "i value is " and "j value is " strings, which are created once and then re-used.
If you do count them, then at least 4, otherwise at least 2.
Actually, each String has its own char[] that actually stores the string. So that's 7 or 3, instead of 4 or 2.
StringBuilder has a char[] as well and might have to create new char[]'s as you add more data to it. String.valueOf or System.out.print might also create objects behind your back and there's no way you can know about them without external tools. Hence "at least".
Upvotes: 1
Reputation: 1582
Only 1 i.e for printing the String .
Thumb rule - Whenever concatenation of strings are done , a new object is created.
Upvotes: 0