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 invoke
d might allocated something themselves, and I have a very strong suspicion that StringBuilder.toString will allocate a String
object.
这将创建一个StringBuilder
对象(以及此对象在内部使用的任何对象),添加值,最后StringBuilder
将创建一个带有结果的String
对象。
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;
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.
只有1
,String对象被连接起来。
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.
Only 1 ie for printing the String .
Thumb rule - Whenever concatenation of strings are done , a new object is created.
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".
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.
5 objects
You should consider 2 points here:
In this case the above answer is correct. Five String objects will be created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.