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); 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); 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 invoked might allocated something themselves, and I have a very strong suspicion that StringBuilder.toString will allocate a String object.
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.
("i value is").concat(i) makes 2 objects. First it has to make "i value is". Then it has to concat i to it to return a new string.obj1="i value is";obj2=obj1.concat(i);obj3="j value is";obj4=obj2.concat(obj3);obj5=obj4.concat(j);"i value is" will already be in existence before the statement runs.("i value is").concat(i) consider i value is as an input rather than an object. Do you have any valid documentation to support your argument.?"i value is" before the concat operation is done.? Do you have any supporting document, i like to learn what's going on inside .!!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".
temp.append(String.valueOf(i))? Since there is an append variant taking an int argument I would assume this is used.String. Just the overall effect is the same.temp.append(i). Edited answer.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.
StringBuilder and StringString 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.
5 objects
You should consider 2 points here:
In this case the above answer is correct. Five String objects will be created.